I would like to add an indeterminate progress bar to the Honeycomb ActionBar, so that any time the user presses "Refresh", the refresh icon temporarily turns into an indeterminate progress bar, until the task completes. The Email app does this already, but I can't figure out how.
Any advice?
To clarify Jon O's answer, the key is to set and unset an action view on the refresh action. This works in both ActionBarSherlock and native 4.x action bar. The following snippet will put the progress indeterminate view on top of the refresh icon, assuming the refresh menu item has ID 'refresh_option' and the replacement layout (which has a ProgressBar) is in layout 'progress_wheel':
MenuItem item = abmenu.findItem(R.id.refresh_option);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View abprogress = inflater.inflate(R.layout.progress_wheel, null);
item.setActionView(abprogress);
Unset the progress view, and the refresh icon will return to visibility:
item.setActionView(null);
See a more detailed example on github.
Hard to tell exactly how the Email app does it, but you may want to stay simple and just call setIcon with the id of a StateDrawable XML file, and then just change the state using a Timer.
To simplify larham1's answer: you don't even need to inflate new action view itself because MenuItem has the method which accepts id of action layout, so you can simply write:
item.setActionView(R.layout.progress_bar);
It turns out that Google has posted an example of doing exactly this as a part of their broader ActionBarCompat compatibility project. Have a look.
You can easily do it by:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
item.setActionView(new ProgressBar(this));
break;
}
return super.onOptionsItemSelected(item);
I'm using the code provided at the original issue here: https://github.com/JakeWharton/ActionBarSherlock/issues/425
Except for android:layout_width and android:layout_height (in the actionbar_indeterminate_progress.xml) I use 32dp; as this was the way it was done in ActionBarCompat:
http://developer.android.com/resources/samples/ActionBarCompat/res/layout-v11/actionbar_indeterminate_progress.html
Related
I am dynamically styling my inflated menu items on my app on onCreateOptionsMenu(). I have a flag which I check and if I want to style them, I proceed. If I do not want to style them, I skip the styling altogether.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
if(mStyleMenu)
styleMenu(menu);
return true;
}
I use invalidateOptionsMenu() to get rid of styling whenever I want. This works on Lollipop and higher system. The style is removed from those menu items. Though, on Kitkat and Jelly Bean (for example), the items keep the style.
I have checked the comment on the invalidateOptionsMenu() and it says the items are recreated. So, I cannot explain why this happens on old systems.
Can someone give me a tip on what is going on?
Thanks.
To modify your menu at runtime, you must implement the onPrepareOptionsMenu function. See Menus.
I want to add buttons to my action bar. How I can do this?
You'd better start with some search and if could not find any answer then ask your question!
By the way:
create an android resource file and name it something like menu_buttons.xml.
Then inflate it and assign to a View object in onCreate method and use it in your ActionBar like:
View v = LayoutInflater.from(yourContext).inflate(R.layout.menu_buttons, null);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(v);
http://www.androidhive.info/2013/11/android-working-with-action-bar/ Refer this link... It explains everything from scratch and you have everything you need to know like adding menu items on action bar... handling click events and what code to add where.. So pls go through this
I am fairly new to android.Recently I have been following this tutorial about making a navigation drawer for my app. Most of it seem pretty straightforward(http://blog.teamtreehouse.com/add-navigation-drawer-android). However,I am confused about what role onOptionItemSelected plays in the activity.
if (mDrawerToggle.onOptionsItemSelected(item)) {return true;}
if I remove the code above , the navigation drawer won't show when I click the toggle icon, and I am curious what is happening behind the scene when I include it and click on the drawer toggle icon.
Also what is the difference between returning false and true...I tried it out but nothing changed.
onOptionsItemSelected() is a method found to return menu item to perform action on it, if somehow its triggered.
check out the documentation for more info.
now why the drewer is not opening when you remove this method?
because it does open it.
how!
as i mentioned earlier this method returned a menu item that you have selected in this case the drawer icon which is located on the action bar.
what is the difference between returning false and true?
here it does perform the action regardless the returned data, it actually doesn't use the returned value so that won't effect the action that have been taken.
hope this will help.
I have an ExpandableListView implementation in my application and I am writing unit tests for it. I'm wondering how I can programmatically perform a click on the child view of an expandable header?
This is NOT a question about how to attach onChildClickListener() to the view. I've found many questions regarding that topic, but I already have that implemented and need to test the functionality of that code when the child view is clicked. I know that I can use the performClick() method to click on the header view to expand/collapse the contents, but I need to perform a click on the sublist of a header.
You can try with:
listView.setSelection(position);
but you have to consider this:
If in touch mode, the item will not be selected but it will still be
positioned appropriately. If the specified selection position is less
than 0, then the item at position 0 will be selected.
For more information you can visit the doc page.
I hope it helps.
Edit
As #RyanM appointed, setSelection just change the selection, but it doesn't perform the "click" action. You can perform the action through the function:
listView.performItemClick(goalListView, itemPosition, itemId);
You can find this function in the class AdapterView.
Sorry for the previous mistake to your answer.
So I have 3 different buttons on my layout. Now as I was creating my layout for phones etc. I decided that having 3 buttons on that page was a waste of space. Those buttons already have a ton of code logic behind them, to set their visibility, replace strings depending on situation etc.
My question is, is there a simple way to change them to a menu, is it possible to simply copy the layout XML I had for them on the main page, and paste it inside a menu?
That menu would open from a simple button, and then present all 3 buttons inside of the menu. So they aren't taking up space the entire time. Will this break the code I already have? Imagine I have the following code. Will it still
bLogin.setText(getResources().getString(R.string.Exit));
bLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
And so on. I'm concerned with the menus behavior, will it close when I click one of the options, will the visibility attributes mess up the men, etc.
You could add a fourth button (let's call it menuButton) that is used to show/hide the other three buttons. You can then add the OnClickListener to the menuButton and set the visibility of the other three buttons from VISIBLE to GONE and the other way around.
So:
Button menuButton = (Button)findViewById(R.id.menuButton);
menuButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (buttonsVisible) {
otherButton1.setVisibility(View.GONE);
...
} else {
...
}
}
});
You could use menu or action bar, which would simplify things for you (you wouldn't have to worry about showing/hiding your buttons, etc). Check the docs. Also, if you're on Android 3.0+, it is recommended to migrate to action bar:
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.
Of course, some work is required, but in the end, you will have a much more flexible solution (eg. if you will later want to add another action...)