Reputation: 6061
Is it possible on Android to have MenuItems (either the ones from the options menu hardware key or action bar -- they are the same anyways) with different styles?
I want one to have a selector applied and another with different selector.
From my search online, I found that you can change the theme and override an attribute with your own style, but that applies to ALL MenuItems.
Can I still set the android:background
property in XML?
Upvotes: 0
Views: 954
Reputation: 15728
It is possible for items of the ActionBar
. You have to define the item that you want to have a different selector as a custom action view.
<item
android:id="@+id/menu_custom"
android:actionLayout="@layout/action_view"
android:showAsAction="always"/>
In the layout referenced by android:actionLayout
you have an ImageView
that you assign your selector and make it clickable and focusable.
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/action_view_background"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:minWidth="56dip"
android:paddingBottom="8dip"
android:paddingTop="8dip"
android:src="@drawable/ic_launcher" />
The click event is not handled by onOptionsItemSelected
. You have to attach an OnClickListener
in onCreateOptionsMenu
.
menu.findItem(R.id.menu_custom).getActionView().setOnClickListener(l);
Upvotes: 2
Reputation: 12497
Quoting this answer
According to the official document at http://developer.android.com/guide/topics/ui/menus.html#checkable
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.
The same is applied to action bar items, since it's the same object, MenuItem
Upvotes: 1