Firstly, my code for setting up the actionBar. (Using default system actionBar, android 4.2+)
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.listing_group, menu);
MenuItem listItem = menu.findItem(R.id.action_list);
ToggleButton customActionIcon = (ToggleButton) getLayoutInflater().inflate(R.layout.custom_action_bar_icon_view, null);
listItem.setActionView(customActionIcon);
customActionIcon.setTextOff("LIST");
customActionIcon.setTextOn("LIST");
customActionIcon.setTypeface(OswaldRegular());
customActionIcon= (ToggleButton)getLayoutInflater().inflate(R.layout.custom_action_bar_icon_view, null);
customActionIcon.setTextOff("MAP");
customActionIcon.setTextOn("MAP");
customActionIcon.setTypeface(OswaldRegular());
MenuItem mapItem = menu.findItem(R.id.action_map);
mapItem.setActionView(customActionIcon);
return super.onCreateOptionsMenu(menu);
}
Layout xml for Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_list"
android:showAsAction="always"
/>
<item
android:id="#+id/action_map"
android:showAsAction="always"/>
</menu>
Layout xml for custom actionBar button:
<?xml version="1.0" encoding="utf-8"?>
<ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_bar_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:background="#color/transparent"
android:textColor="#drawable/listing_group_activity_actionbar_text_color_selector">
</ToggleButton>
The Problem:
When the screen with this action bar first loads, both the buttons show up in the deselected color, but they both show the default text of "Off". When I click one of them, they correctly change color AND they change the text to the one I set in onCreateOptionsMenu(). i.e. , one becomes LIST and one becomes MAP . And then they continue to stay that way and function as normal ToggleButtons would. I would like them to start with the correct text showing, and after that is fixed I want one of them to be selected by default.
Any help on the matter is much appreciated! Thanks!
Fixed. it. Had to do customActionIcon.setChecked(true) for the enabled one and setChecked(false) for the disabled one and they start with the right text and right state
Related
about android Toolbar, how to hide default setting menu in toolbar
in the this activity i hava set a single icon menu,like this –
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title=""
android:id="#+id/menu_add"
android:icon="#drawable/actionbar_add_icon"
android:showAsAction="ifRoom">
</item>
</menu>
it is menu/main_home.xml
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
return super.onCreateOptionsMenu(menu);
}
but it show a three dots menu. i want know why
and how to show sub menu icon image ,it is normal in the Actionbar, but hide in the Toolbar
If you want to empty the contents of the menu, go to the res/menu folder and delete the item tags from the menu_main.xml file. You will also need to delete references to the items in onOptionsItemSelected(MenuItem item). The menu dots will disappear if there are no items.
If you want to completely remove the three dots menu, then go to the onCreateOptions(...) method in your code and delete it, or simply remove the getMenuInflater().inflate(...); portion of it. You can safely remove the onOptionsItemSelected(MenuItem item) method as well because it will have no purpose.
The simplest way to make the menu disappear is to have the onCreateOptions(...) method return false.
this question is solved use app:showAsAction ="ifRoom" not android:showAsAction ="ifRoom"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:title=""
android:id="#+id/menu_button"
app:actionLayout="#layout/menu_single_button"
app:showAsAction = "always"
/></menu>
My action bar menu items are cutting off the width of the action bar Spinner so that the Spinner's text gets truncated. I've set all my menu items to use "ifRoom" but unfortunately they interpret the original Spinner width as available room.
Here's the truncation when I have three menu items (yes, I know the third icon looks identical to the second):
Here's what I get when I comment out the third menu item. Ultimately, I want something that looks like this, but with an overflow icon showing instead of the search icon:
I populate the Spinner using a custom adapter (extending from BaseAdapter and implementing SpinnerAdapter).
Here's my menu XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/ab_refresh"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/ab_refresh"
android:icon="#drawable/navigation_refresh"/>
<item
android:id="#+id/ab_search"
android:orderInCategory="2"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/ab_search"
android:icon="#drawable/action_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"/>
<item
android:id="#+id/ab_toggle"
android:orderInCategory="3"
android:showAsAction="ifRoom"
android:title="#string/ab_latest"
android:icon="#drawable/action_search"/>
</menu>
And here's my ab_dropdown XML (used in getView of my custom adapter):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="fill_horizontal">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginTop="1dp" />
</RelativeLayout>
What I've tried:
Setting a minWidth of 570dp for the RelativeLayout and TextView of ab_dropdown. No effect regardless of whether I set it in XML or the getView method of my custom adapter.
Hardcoding the layout_width of RelativeLayout and TextView to 570dp. Again, no effect in XML or code.
I WOULD attempt to find the action bar Spinner programmatically so as to set its width, but when I tried to do this for a different reason, I couldn't.
Any ideas?
Personally, I'd switch to a navigation drawer and dump the drop-down list navigation.
That being said, it sounds like you need two versions of your menu resource. Have the third item be never in the toolbar (always in overflow) by default, and have it be ifRoom on larger screens (e.g., res/menu-sw600dp/).
You could also experiment with AutoScaleTextView for your drop-down list navigation labels.
The navigation portion of the action bar is deemed least important, which is why ifRoom considers there to be room.
On a Galaxy S3, I have an issue where a button in the ActionBar is configured as showAsAction="always" and being shown on both the hardware overflow menu and the Action buttons. I would like it to not be shown on the hardware overflow menu and only on the Action buttons. I can disable the menuitem in the onCreateOptionsMenu but it will hide the button on both places.
Something to note: if I force the "3 dots" Action Overflow menu to show, the refresh button gets properly hidden from the hardware overflow menu but still doesn't get hidden from the hardware overflow menu.
Something else to note: if I call menu.size() in either onCreateOptionsMenu or onPrepareOptionsMenu, it doesn't reflect the extra button. For example, I have four buttons and the first button is being shown in both the Action buttons and the overflow menu. menu.size() still returns 4 and doesn't seem to realize that it is showing an extra button.
I can't post a screenshot because this is an app for a client but here is my actionbar.xml file. The refresh button shows in both the overflow and the action bar at the top.
actionbar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/refreshmenuitem"
android:icon="#drawable/refreshicon"
android:title="Refresh"
android:showAsAction="ifRoom"
android:visible="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<item android:id="#+id/helpbutton"
android:title="Help"
android:showAsAction="collapseActionView"
android:icon="#drawable/ic_action_helpbutton" />
<item android:id="#+id/settingbutton"
android:title="Settings"
android:icon="#drawable/ic_action_settingbutton"
android:showAsAction="collapseActionView" />
<item android:id="#+id/importbutton"
android:title="Import file"
android:icon="#drawable/ic_action_importbutton"
android:showAsAction="collapseActionView" />
</menu>
So I figured it out but it is kind of a hack. So if you have a phone such as the Samsung Galaxy S3 that has a hardware menu button, the onCreateOptionsMenu function actually gets called twice. Once for when the Activity gets loaded to load any menu items that should display in the top right and another time when the user presses the hardware menu button. All I did was create a variable called _firstTimeOnCreateOptionsMenu that is set to false after the first time it is onCreateOptionsMenu method:
public boolean onCreateOptionsMenu(Menu menu)
{
// Populates the actionbar/Menu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbarmenu, menu);
boolean hardware = false;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
hardware = ViewConfiguration.get(context).hasPermanentMenuKey();
}
MenuItem b1 = menu.findItem(R.id.refreshmenuitem);
if(!hardware || _firstTimeOnCreateOptionsMenu)
{
b1.setVisible(true);
_firstTimeOnCreateOptionsMenu = false;
}
else
{
b1.setVisible(false);
_firstTimeOnCreateOptionsMenu = true;
}
}
Hopefully this helps anyone else who is having this issue.
Is your targetSdkVersion set to 14?
http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html
I'd be a bit surprised if this was the issue, since you'd expect unknown XML to just be ignored, but according to the documentation <item> does not support android:layout_width and android:layout_height.
I want to have webview displayed fullscreen with action bar on top of it and standard menu bar on bottom. It's displayed correctly without action bar. When I am turning it on, bottom part of webview is displayed under bottom menu bar and I want to fit it between both menu bars.
Its probably easy fix but can't find answer on that.
Thanks!
Code:
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layerType="software"
/>
</RelativeLayout>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/group1">
<item android:id="#+id/opt" android:android:showAsAction="ifRoom|withText" android:enabled="true" android:title="Option 1" android:visible="true"></item>
<item android:id="#+id/opt2" android:showAsAction="ifRoom|withText" android:enabled="true" android:title="Option 2" android:visible="true"></item>
</group>
</menu>
activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings, menu);
this.menu = menu;
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
return true;
}
android:windowActionBarOverlay Declares whether the action bar should
overlay the activity layout rather than offset the activity's layout
position This is false by default.
When overlay mode is enabled, your activity layout has no awareness of
the action bar laying on top of it.
More about Actionbars can be read here: http://developer.android.com/guide/topics/ui/actionbar.html
Use Relative Layout in order to fit things in the right way.
Show us more code in order to obtain for a better answer :D
MOD
Options menu is not made usually for fit into your layout , but overlapping it . I tell you to consider creating a fixed real menu which hides whenever you want with some action.
Also think that menu can't have a fixed size and it may change with orientation , mobile phone , android version . Then if you could made what you need , you'll have more problems with your layout than making a custom one.
Sometimes when doing some so very simple, you miss something big. I must be missing something huge because I am getting nowhere fast (and an hour of sifting through the web has revealed nothing).
I want to have a menu with items with checkmarks in group--just like a simple RadioGroup layout. I get the menu, but no checkmarks of any kind.
Here's the res/menu/options_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:checkableBehavior="single">
<item
android:id="#+id/item1"
android:title="item1"
/>
<item
android:id="#+id/item2"
android:title="item2"
/>
<item
android:id="#+id/item3"
android:title="item3"
android:checked="true"
/>
</group>
</menu>
And of course, here's the relevant methods in my Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
This is about as simple as I can make it--any ideas on what I'm missing?
I think you try to create Options Menu (it's displayed by click menu button) and according to this
Note: Menu items in the Icon Menu (from the Options Menu) cannot
display a checkbox or radio button. If you choose to make items in the
Icon Menu checkable, you must manually indicate the checked state by
swapping the icon and/or text each time the state changes.
you can't add group menu in OptionsMenu. so i think you shoud use Context Menu or Submenu.
Take a look at this article
Here's the work-around:
In your strings.xml file you can embed a unicode checkmark. There are two to choose from. For this project I prefer the friendlier check of \u2714. You then swap a string with the check-mark for a string without it via onPrepareOptionsMenu() as appropriate.
Here's the xml code two strings, one with and one without checkmarks:
<string name="opp_random">unchecked</string>
<string name="opp_random_check">\u2714 checked</string>
Happy coding!
How about
android:checkable="true"