how to apply animation on android activity - animation

i am working on app in which i need some animation on activity i cannot find
any solution. please any one help me
public class MainActivity extends Activity implements AnimationListener {
TextView tv;
Animation animFadein;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
}
}

Animations can be performed through either XML or android code. In this tutorial many of animation are explained that how to do animations using XML notations for an activity.
tutorial link
and source code

Related

Several Xamarin.Forms Android activities

I want to use several Android activities. First is general application. Second is notification view. They have different activity settings and therefore I can't use one activity for this issue.
I try to do this:
[Activity(Label = "Life Manager", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Forms.Init(this, bundle);
ActionBar.SetIcon(new ColorDrawable(Color.Transparent));
LoadApplication(new TimeManagerApplication());
Device.StartTimer(new TimeSpan(0, 0, 0, 5), OpenNotificationActivity);
}
private bool OpenNotificationActivity()
{
Intent intent = new Intent(this, typeof(NotificationActivity));
StartActivity(intent);
return false;
}
}
[Activity(Label = "NotificationActivity")]
public class NotificationActivity : FormsApplicationActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
RequestWindowFeature(WindowFeatures.NoTitle);
Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.KeepScreenOn);
base.OnCreate(savedInstanceState);
Forms.Init(this, savedInstanceState);
LoadApplication(new NotificiationApplication());
}
}
In this line:
LoadApplication(new NotificiationApplication());
I take an error:
System.NullReferenceException: Object reference not set to an instance of an object.
How can I use two android activities in one application and use cross-platform Xamarin.Forms views for it?
Update:
Without these lines application perfectly works:
//RequestWindowFeature(WindowFeatures.NoTitle);
//Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.KeepScreenOn);
But how can I hide status bar and use fullscreen view?
I decided this issue.
To take Fullscreen view without status bar is enough use only Window Flags
Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.KeepScreenOn);
without:
RequestWindowFeature(WindowFeatures.NoTitle);
Application works as expected without exceptions.

requestFeature() must be called before adding content in Xamarin Forms application

I am developing one sample login application using xamarin forms, I developed code in common portable project. When running in Xamarin forms android project, this error is displayed:
Android.Util.AndroidRuntimeException: requestFeature() must be called before adding content
The code is:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (new App());
}
}
How can I overcome this?
inherit your MainActivity from FormsAppCompatActivity instead of FormsApplicationActivity
https://github.com/xamarin/Xamarin.Forms/issues/13779

android buttons and image in the same page

I have to create a button to show an image. I created it using two activity, but I want that the button and the image view stay in the same page. So, I think I have to create only one activity, right? can you explain hot to do this? this is my code:
ACTIVITY ONE:
public class HomeWork extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_work);
Button getResultButton = (Button) findViewById(R.id.btn1);
getResultButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent resultIntent =new Intent(HomeWork.this,HomeWork2.class);
startActivity(resultIntent);
}
});
}
SECOND ACTIVITY:
public class HomeWork2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_work2);
ImageView img=(ImageView)findViewById(R.id.imageView);
img.setBackgroundResource(R.drawable.apple);
}
the apk works correctly, the only problem is that I want button and imageview in the same image
Create a layout which contains both the Button and the ImageView.
Then in the Button's click listener, use
ImageView img=(ImageView)findViewById(R.id.imageView);
img.setBackgroundResource(R.drawable.apple);
instead of
Intent resultIntent =new Intent(HomeWork.this,HomeWork2.class);
startActivity(resultIntent);
Also remove (the now unused) HomeWork2 from your manifest.
[EDIT]
You may add more buttons, to change the contents of the ImageView.
You only have to put in their click listeners:
img.setBackgroundResource(R.drawable.orange);
or
img.setBackgroundResource(R.drawable.pineapple);
or ...

Slow loading of layout

I have a super class which is in a library. This library take care of initializing some basic layout components and other stuff. My problem is that it takes 1.x seconds to load the layout, and shows the default layout for a while, before setting the child-specified layout.
This is the method of my super class:
public void InitializeWindow(Activity act, int layoutResourceId, String windowTitle,
Object menuAdapter, int slideMenuMode) {
super.setContentView(layoutResourceId);
super.setBehindContentView(R.layout.menu_frame);
this.menuAdapter = menuAdapter;
this.slideMenuMode = slideMenuMode;
setWindowTitle(windowTitle);
initializeSlidingMenu();
}
This is called this way:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.InitializeWindow(this, R.layout.activity_home, "\t\tHome",
new MenuAdapter(this, R.menu.slide_menu), SlidingMenu.TOUCHMODE_FULLSCREEN);
}
The application works like a charm, but it takes, as I said around 1.x seconds to load the layout passed from the child-class. Why does this happen?
By request, this is my initializeSlideMenu() method:
public void initializeSlidingMenu() {
this.setSlidingActionBarEnabled(true);
getSlidingMenu().setBehindOffsetRes(R.dimen.actionbar_home_width);
getSlidingMenu().setShadowWidthRes(R.dimen.shadow_width);
getSlidingMenu().setShadowDrawable(R.drawable.shadow);
getSlidingMenu().setTouchModeAbove(slideMenuMode);
getSlidingMenu().setBehindScrollScale(0.25f);
ListView v = new ListView(this);
v.setBackgroundColor(Color.parseColor("#000000"));
v.setAdapter((ListAdapter) menuAdapter);
getSlidingMenu().setMenu(v);
}
To avoid such problems there are three ways in general.
Let your onCreate() finish after setContentView() call as early as possible. You can use postDelayed runnable to delay few initialization which may not be needed at early stages.
Do some task when the view is ready, it causes the Runnable to be added to the message queue of that view.
Snippet
view.post(new Runnable() {
#Override
public void run() {
}
});
If none of the above helps consider "Optimize with stubs" link : http://android-developers.blogspot.in/2009/03/android-layout-tricks-3-optimize-with.html
Hope it helps.
I suspect that the trouble spot for you is with:
v.setAdapter((ListAdapter) menuAdapter);
You should do this as part of an AsyncTask. It will often be very slow to execute the loading by the adapter.
Here is a snippet from a sample AsyncTask implementation:
//before starting the load, I pop up some indicators that I'm doing some loading
progressBar.setVisibility(View.VISIBLE);
loadingText.setVisibility(View.INVISIBLE);
AsyncTask<Void, Void, Void> loadingTask = new AsyncTask<Void, Void, Void>() {
private ArrayList<Thing> thingArray;
#Override
protected Void doInBackground(Void... params) {
//this is a slow sql fetch and calculate for me
thingArray = MyUtility.fetchThings(inputValue);
return null;
}
#Override
public void onPostExecute(Void arg0) {
EfficientAdapter myAdapter = new EfficientAdapter(MyActivity.this, thingArray);
listView.setAdapter(myAdapter);
//after setting up my adapter, I turn off my loading indicators
progressBar.setVisibility(View.INVISIBLE);
loadingText.setVisibility(View.INVISIBLE);
RelativeLayout layout = (RelativeLayout)MyActivity.this.findViewById(R.id.spacey);
if (layout != null) {
LayoutInflater inflater = LayoutInflater.from(MyActivity.this);
View view = inflater.inflate(R.layout.name_tabled_sub, layout);
NamedTableView tableView = new NamedTableView(MyActivity.this, view);
}
progressBar.setVisibility(View.INVISIBLE);
loadingText.setVisibility(View.INVISIBLE);
}
};
loadingTask.execute();
You can also do "PreExecute" items with the Async task, as well as update.

Shared Toast does not show in Android 3.0.1 on Motorola Xoom

I use a shared Toast across different Activities in order to only show the latest message, immediately discarding any previous ones. I put the code in the custom Application object:
public class GameApp extends Application {
private Toast mToast;
#Override
public void onCreate() {
super.onCreate();
mToast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
}
public void displayToast(int textId) {
displayToast(getText(textId));
}
public void displayToast(CharSequence text) {
mToast.cancel();
mToast.setText(text);
mToast.show();
}
}
The Toast showed up on my 1.6, 2.2, and 3.0 emulators. But when I downloaded the released app from the Market, it only shows on my G1 (CyanMod 6.1) but not Xoom (3.0.1). I tried connecting the Xoom with USB debugging, but nothing relevant showed up in LogCat.
Prior to this, I used to do Toasts the conventional way (i.e. via Toast.makeText()) and that worked on everything as expected.
Could there be any potential problem with my above code, or could this be a bug in the Xoom? Here is the link to my app, in case you want to test it. The Toast should show up when you click Today, Progress in the Main screen. I appreciate any help. Thank you very much :)
i'm not sure but the sdk that motorola uses may be different.. and mToast.cancel() could be doin something terrible.. so have you tried this..
public void displayToast(CharSequence text) {
mToast.setText(text);
mToast.show();
}
This is because that mToast.cancel(); may close the toast if it's showing, or don't show it if it isn't showing yet.
Please create new Toast object when users click buttons. And keep the previous Toast object reference. Next time when user click buttons, cancel the previous Toast object and create new Toast again.
public class GameApp extends Application {
private Toast mToast;
private Context mContext;
#Override
public void onCreate() {
super.onCreate();
mToast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
}
public void displayToast(int textId,Context mContext) {
this.mContext = mContext;
displayToast(getText(textId));
}
public void displayToast(CharSequence text) {
mToast.cancel();
mToast = new Toast(mContext);
mToast.setText(text);
mToast.setDuration(Toast.LENGTH_SHORT);
mToast.show();
}
}

Resources