Android custom menu

Menu is a useful interface component in applications. The default option menu provided by Android does not allow to show Unicode text. However, sometimes, you need items of the menu to display Unicode text to the user. In this tip, I will show you how to customize option menu to show a button that contains an icon and Unicode text. On Android 3.0 and higher, the option menu is presented by the action bar.

To get start, you need to create a new Android project. Then in the res/menu folder create a custom menu xml file called custommenu.xml as shown below. This custom menu has only one item. It is a button item. This item will show an icon and Unicode text.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=""
  <item android:id="@+id/item1"

As you don't need the default option menu, in the onCreateOptionsMenu method of the MainActivity class comment or remove the following line:

getMenuInflater().inflate(, menu);

After that you need to write more code as shown below. In this code, you get the MenuInflater object by using getMenuInflater(). Then you call the inflate method of the MenuInflater to inflate the custom menu. After you inflated the custom menu, you can make a reference to the item of the menu. When you obtain the reference to the menu item, you will be able to customize the item to show icon and Unicode text. Your icon image file has to be stored in the res/drawable folder. An embedded Unicode font must be stored in the assets folder.

   public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     //getMenuInflater().inflate(, menu);
   //Use custom menu
   MenuInflater inflater = getMenuInflater();
   //Inflate the custom menu
   inflater.inflate(, menu);  
   //reference to the item of the menu
   MenuItem i=menu.findItem(;
   Button itemuser =(Button) i.getActionView();
   // Create Typeface object to use unicode font in assets folder
   Typeface fontUnicode= Typeface.createFromAsset(this.getAssets(), "Hanuman.ttf");
   // Set unicode font to menu item
   // Set item text and color
   // Make item background transparent
   // Show icon next to the text
   Drawable icon=this.getResources().getDrawable( R.drawable.user);
   itemuser.setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );

return true;


Posted by: Dara | post date: 05-18-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.