ActionBar refresh - android

I am using Android compat library to show a ActionBar at the top of the screen.
The actionBar contains a refresh button which is a rotating circle if it is pressed.
To activate the rotation of the circle in the actionBar I use following:
getActionBarHelper().setRefreshActionItemState(true);
This works fine, if I call it via a button click or via onOptionsItemSelected().
But if I call it in onCreate or onStart nothing happens. I am wondering why?

Remember that the rotating circle only appears once onCreateOptionsMenu has been executed. It means that it will not appear if you try to execute getActionBarHelper().setRefreshActionItemState(true) on your Activity "onCreate". If you try on "onResume" it will work. You can use it wherever you need once onCreateOptionsMenu has been executed.
I hope it helps you.

Related

Android ActoinBar homeAsUp only works once

I'm working on an app that only uses a single Activity and switches out fragments as they are needed with the navigation drawer.
Now we want to navigate back from one of these fragments by using the homeAsUp button in the ActionBar.
I have followed all the steps to set the button up. From disabling the navigation drawer setDrawerIndicatorEnabled(false) and calling setDisplayHomeAsUpEnabled(true) in the fragment's onCreateView().
I also set setHomeButtonEnabled(true) in the MainActivity's onCreate() however because the app is already in the MainActivity, we cannot specify a Parent Activity.
Whenever I run a fresh install of the app, the homeAsUp button works and is registered in the onBackPressed(), not onOptionsItemSelected() method. However, when I close the app and run it again the button does not even register clicks.
In onBackPressed() I check a few conditions, but it does not block the button press.
In onOptionsItemSelected() I check for android.R.id.home.
Unfortunately, I cannot post the code.
This post describes what I'm trying to achieve: Switching between Android Navigation Drawer image and Up caret when using fragments
I managed to fix the issue I was experiencing. It was a very simple mistake.
Because I'm not the original author of this code, I went through the MainActivity thoroughly.
As it turns out the original author called setDisplayHomeAsUpEnabled in the onCreate function (which is extremely long), but near the end he also called setSupportActionBar, which made the first call of setDisplayHomeAsUpEnabled useless. Moving setDisplayHomeAsUpEnabled below setSupportActionBar fixed my problem.
If you did everything correctly, make sure that your code is written in the correct order.
Furthermore, if you use custom toolbars in other fragments, remember to set your original Support Action Bar by calling setSupportActionBar(toolbar.find(this)) in onResume of your MainActivity.

OnCreateOptionsMenu not called when Style set to a Dialog Theme

I have an activity in Android. Specifically, I am using Xamarin.Android, formerly monodroid. I have an activity. When it is running on a phone (small screen device), we want the activity to be full screen. When it runs on a tablet, we want the activity to have a "border" around similar to a dialog. I do a programmatic check to get the screen dimensions and then determine if we are on a tablet or phone. If we are on a phone, I call an activity's SetTheme method and pass in a dialog theme. I have tried several dialog themes with no difference. My menu items do not show. I track this down and my overridden OnCreateOptionsMenu method is not called. I know because I set a breakpoint in the first line of my activty's OnCreateOptionsMenu method and the breakpoint is not hit.
The code that I am using to set the theme is:
this.SetTheme (Android.Resource.Style.ThemeDialog);
I've also tried this from the Activty's theme attributes, but I am getting the same result.
I'm looking for any ideas, thoughts, whatever.
Thanks for your time.
Wally
By default the dialog theme does not have an actionbar. No reason to create menu items without it (on newer android versions).
Call RequestWindowFeature (WindowFeatures.ActionBar); in onCreate to get an actionbar in a dialog themed activity. Then OnCreateOptionsMenu will be called

setBackgroundDrawable not working properly [duplicate]

This question already has answers here:
Actionbar background is not being changed
(2 answers)
Closed 8 years ago.
In my application, I am using actionbarsherlock. I use
getSupportActionBar().setBackgroundDrwable(R.drawable.test).
It works fine, the color is showed as expected, however when I go to another activity and go back to the main activity. The bar color is changed to black (in my program, I dont set black for the backgrounddrawble and just call setSupportAction().setBackgroundDrawable in onStart()). However, if I change the code to
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff550000)));
The app works fine. When I click on back button, the color is set to 0xff55000 which is expected.
I will suggest not to use the color code directly this way while you are trying to set the color programmatically. It will not take it. just try out this way..
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(mContext.getResources().getColor(Color.BLACK)));
Hope it will work for you.
It doesn't work well for me either, specially if I use with the navigation drawer toggle list. In my case, if i don't use the toggle, the background goes white during a second and then load the background drawable. If I use the toggle, the background remains white.
I had to use a custom view to achive the effect I wanted to get instead of changing the background.
I have almost the same issue, it happens that calling getActionBar().setBackgroundDrawable twice does not work.
If I only call it only once in the onCreate is works. If I put the code in the onResume, it works for the first onResume, but it is set to grey for any other onResume

NullPointerException Android ActionBar Back (but not Menu Bar Back)

I get an error with the ActionBar back button when I'm moving back to a previous activity, but when I use the Menu Button Back, everything works as planned! I think I have an idea of what is going on, because using the Action Bar back button causes a recreation of the previous activity, but I don't want that to happen. How do I override the Action Bar (default) back button to perform like the Menu Bar back button so that OnDestroy() is not called?
I figured it out, I had to call finish() in my onMenuItemClicked function rather than using the default function.
This answer may be outdated though.

Android toast appearing in each tab

I currently have a tabview and in my first tab i have a toast message that appears. If click on another tab while the toast is still displaying, it will display in that tab as well. Is there any way to not make this happen? Do i simply cancel it inside the onPause()? thanks for your help
You could detect tab change and cancel it in that method. For tab change help - Android TabWidget detect click on current tab
Toast's are not tied with any particular activity as they are an overlay over the screen. If you want to explicitly close the Toast, you need to pass a handle of it to whichever activity needs to close it and call Toast.cancel().
Yeah you can simply call cancel() in onPause() of the activity that has created it.

Categories

Resources