How to animate home button in action bar? - android

I'm using ActionBarSherlock and I would like to animate the home button to tell the user to click on it. I want to use three different images to show the animation.
How can I do this?

Get the action bar in you onCreate:
ActionBar actionBar = getSupportActionBar();
Create and load an animation drawable which you defined in XML:
// homeDrawable is a field on your activity
homeDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.my_thing);
Set the drawable as the icon on the action bar:
actionBar.setIcon(homeDrawable);
Post a Runnable to start the animation when the main thread is clear:
getWindow().getDecorView().post(new Runnable() {
#Override public void run() {
homeDrawable.start();
}
});
Don't forget to stop the animation at some point!

Related

How to replace the actionbar by a popup menu?

I want to achieve 1 of these options for my EditText :
Replace the actionbar that appear on the top by a popup menu instead. Something like this for exemple:
Or make the actionbar floating and child of my current view (in some way same a first option)
i need this because i add my view via windowManager.addView(view, Layout_Params); and in this way i have some trouble with the actionbar on the top (it is displayed blank)
actually i do this to show the actionbar :
#Override
public ActionMode startActionMode(ActionMode.Callback callback) {
Activity host = (Activity) this.getContext();
return host.getWindow().getDecorView().startActionMode(callback);
}
but it's don't work, it's show me an empty white actionbar on the stop instead :( i think i need to create myself the ActionMode but i don't know how to do it.
Ok. You can hide the actionbar when your edittext is gained focus. Then you need to show the popup menu where ever you want.
EditText t = new EditText(this);
t.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b){
getSupportActionBar().hide();
//now show your popup menu at the top position
}else{
getSupportActionBar().show();
//here you dismiss the menu.
}
}
});

Changing color of action bar doesn't work in ICS

I am changing the colour of action bar using the following code.
actionBar= getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(backgroundColor));
This works perfectly fine in onCreate() method(i.e when my activity starts).
But when I am using the same code in Android 4.1 to change color of action bar in onClick() method(i.e when user clicks say a button) then it changes the color of action bar to 'white' ONLY whatsoever be the value of backgroundColor.
In Android 2.3 it works perfectly fine in both cases, whether called from onCreate or onClick().
It seems to be dead end for me.
It seems like for Android 4.0 up to Android 4.1 (4.2 is ok) you cannot change the action bar background once the activity has started.
So I guess a solution in those cases is to restart the activity with a different background color.
Not very elegant but thats the best I can come up with.
Something like this perhaps:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
if (getIntent().hasExtra("color")) {
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(getIntent().getIntExtra("color", 0)));
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent it = getIntent();
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
it.putExtra("color", Color.BLUE);
startActivity(it);
}
});
}
A workaround I used to solve this problem was to maintain a reference to the ColorDrawable that was used to set the background initially. Then, to change its colour, modify the colour of the Drawable, and then tell the activity to invalidate its options, which causes the ActionBar to be redrawn. Here's my code to set the action bar color:
ColorDrawable sBackgroundDrawable;
...
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void setActionBarColor(int color){
if(getSherlockActivity() != null && !mCallback.isNavDrawerShowing()){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN){
if(sBackgroundDrawable == null){
sBackgroundDrawable = new ColorDrawable(color);
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(sBackgroundDrawable);
}else{
sBackgroundDrawable.setColor(color);
getSherlockActivity().invalidateOptionsMenu();
}
}else{
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
}
}
}
Based off of what steve_the_kid mentioned I was able to solve this by saving the color drawable.
ColorDrawable actionBarBackground = new ColorDrawable();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setBackgroundDrawable(actionBarBackground());
actionBarBackground.setColor(someColor);
}
void onSomethingClick() {
actionBarBackground.setColor(someOtherColor);
}
Sorry for such a late reply,But to help others I am replying to this question after so long.
Just set the Title of the action bar or make change in the icons or menu along with changing the colorDrawables.A simple change will recreate the action bar with new color.Even if you need same title or icon every time set same title or same icon.
I hope this will help others

show action bar after activity animation completes

I am using the technique here to animate an activity into life. But I don't want the action bar to show until after the animation is complete. If I set action bar to hide and then to show in onResume, it's as if I never hid it. does anyone know how to do this?
Create an animation and set a callback:
Animation anim = AnimationUtils.loadAnimation(context,R.anim.an_animation);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationEnd() {
// code to show actionbar
}
public void onAnimationStart() {}
public void onAnimationRepeat() {}
}
and then proceed to add the animation to the view and start it
EDIT: I just read you meant to animate an Activity, not a View. Since Activity does not provides any method to set animation callbacks, you could create an AyncTask, in the onCreate method of the second Activity, that will wait the same time as the animation. If the animation goes for 500ms, then your AsyncTask will wait 500ms and then shows the ActionBar:
public class SecondActivity {
private ActionBar bar;
protected void onCreate(Bundle savedInstanceState) {
bar = getActionBar();
bar.hide();
new AsyncTask<Void,Void,Void>() {
#Override
public void doInBackground(Void params...) {
Thread.sleep(500);
bar.show();
return null;
}
}.execute();
}
}

How to hide ActionBar and NavigationBar after a certain delay time?

I would hide navigation bar and action bar of my app after some second that display don't get touched, and expand current view to fullscreen.
Then if user touch the screen (or better if he swipe down), make visible both again. How to?
You can use a Handler to delay certain actions.
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
// DO DELAYED STUFF
getActionBar().hide();
}
}, delaytime); // e.g. 3000 milliseconds
The actions you take inside the run() method will be executed after
the delay-time you set.
If you are using Eclipse, just create a new project and select "Fullscreen Activity": this is a good example on how to do what you want.

How can we display progress icon in button

I need to display progress icon in button so that user can interact other GUI elements while background task is processing.
I have searched in Android developer site and found that we can use animated drawables but don't know how to use them. Please advise on the same.
The very simple way to do this without using the animated drawable is to use "PregressBar" component in the design layout xml. When u need to show it, just set it's visibility property to visible and when you need to hide it, u can set it's visibility property to GONE. But remember this is UI task so when u need to do this with non-UI thread, u need to use Handler to set the status of "ProgressBar" component at runtime.
Below id the component in the layout file.
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
Below is the code written in java file
ProgressBar prg;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
prg=(ProgressBar)findViewById(R.id.ProgressBar1);
prg.setVisibility(ProgressBar.GONE);
}
public void start_background_process()
{
// starting the process
prg.setVisibility(ProgressBar.VISIBLE);
new Thread(new Runnable()
{ public void run()
{
// Do your background stuff here which takes indefinite time
mHandlerUpdateProgress.post(mUpdateUpdateProgress);
}
} ).start();
}
final Handler mHandlerUpdateProgress= new Handler();
final Runnable mUpdateUpdateProgress = new Runnable() {
public void run() {
// ending the process
prg.setVisibility(ProgressBar.GONE);
}
};
If the default progress indicator is good enough for you (i.e. the spinning wheel), then you can just use ProgressBar. To change it from a normal progress bar to a spinning wheel, use progressBar.setIndeterminate(true).

Categories

Resources