Reputation: 423
I just added a Menu to my activity. Additionally I want to set some icons to each menu item. Therefore I tried both these methods but I do not see these icons on the device. What am I doing wrong ?
I tried the XML way:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/help"
android:icon="@drawable/help"
android:title="@string/menu_help" />
<item android:id="@+id/settings"
android:icon="@drawable/settings"
android:title="@string/menu_settings" />
<item android:id="@+id/num"
android:icon="@drawable/num_icon"
android:title="@string/menu_num" />
</menu>
Method 2: In my code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//Set icon for the menu button
Drawable num_icon = getResources().getDrawable(R.drawable.num_icon);
menu.getItem(2).setIcon(num_icon);
Drawable settings_icon = getResources().getDrawable(R.drawable.settings);
menu.getItem(1).setIcon(settings_icon);
Drawable help_icon = getResources().getDrawable(R.drawable.help);
menu.getItem(0).setIcon(help_icon);
return true;
Also, could anyone tell me how big should these icon sizes be? Mine is256x256 pix. Thanks
Upvotes: 1
Views: 7533
Reputation: 157
You just have need to small changes in your manifest file according to your need you can change theme I get the solution for above problem with small change i.e.even using any version of android it will work for (2.3 to latest version 4.4 kitkat also).
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.WithActionBar">
Upvotes: 1
Reputation: 2290
use following code it may help you.. menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="@+id/menu_bookmark"
android:icon="@drawable/icon_bookmark"
android:title="Bookmark" />
<item android:id="@+id/menu_save"
android:icon="@drawable/icon_save"
android:title="Save" />
<item android:id="@+id/menu_search"
android:icon="@drawable/icon_search"
android:title="Search" />
<item android:id="@+id/menu_share"
android:icon="@drawable/icon_share"
android:title="Share" />
<item android:id="@+id/menu_delete"
android:icon="@drawable/icon_delete"
android:title="Delete" />
<item android:id="@+id/menu_preferences"
android:icon="@drawable/icon_preferences"
android:title="Preferences" />
</menu>
Now open your main Activity class file and put following code MenusActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidMenusActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Upvotes: 1
Reputation: 3645
In the android sdk menu icons are of the following sizes:
ldpi 32 x 32 px mdpi: 36 x 36 px hdpi: 48 x 48 px xhdpi: 64 x 64 px
256x256 is too large.
Upvotes: 1