Android Option Menu With Icon
How to show icon with option menu.I have tried the following code but my option menu is without image icon.I am using android version 4.0 for developing app. Java code : public bo
Solution 1:
Override OnPrepareOptionsMenu
and add icon from there too
and if its for above 3.0, use android:showAsAction
in xml.
eg. android:showAsAction="ifRoom|withText"
Solution 2:
I tried the code in two line and it works:
publicbooleanonCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("Add Contacts");
menu.getItem(0).setIcon(R.drawable.ic_launcher);
returntrue;
}
Solution 3:
You can create a custom menu like this:
<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/add_contacts"android:icon="@drawable/ic_launcher"android:title="@string/add_contacts"
/></menu>
And then inflate it
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
MenuInflaterinflater= getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
returntrue;
}
More on this here: http://developer.android.com/guide/topics/ui/menus.html#options-menu
Solution 4:
you can directly set this into the xml file.
<item android:id="@+id/add_contacts"
android:icon="@android:drawable/plus_icon"
android:title="Add Contacts"/>
Solution 5:
You Can try Following this Link.
Check this out and tell me if it worked or not.
Or you can do some thing like this. Create menu.xml
<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/next"android:icon="@drawable/ic_next"android:title="@string/next" /><itemandroid:id="@+id/previous"android:icon="@drawable/ic_previous"android:title="@string/previous" /><itemandroid:id="@+id/list"android:icon="@drawable/ic_list"android:title="@string/list" /></menu>
And now you will be able to set ICON on menu
Now in CreateOptionMenu
publicbooleanonCreateOptionsMenu(Menu menu) {
MenuInflaterinflater= getMenuInflater();
inflater.inflate(R.menu.menu, menu);
returntrue;
}
And to access that menu.
publicbooleanonOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.next:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.next) + " menu option",
Toast.LENGTH_SHORT).show();
returntrue;
…
default:
returnsuper.onOptionsItemSelected(item);
}
}
Post a Comment for "Android Option Menu With Icon"