Inflating XML Menu Resource Files

Let us assume that the name of this XML file is my_menu.xml. You will need to place this file in the /res/menu subdirectory. Placing the file in /res/menu automatically generates a resource ID called Resource.menu.my_menu.

Now let's look at how you can use this menu resource ID to populate the options menu. Android provides a class called android.view.Menulnflater to populate Menu objects from XML files. We will use an instance of this Menulnflater to make use of the Resource.menu.my_menu resource ID to populate a menu object:

^Override public boolean onCreateOptionsMenu(Menu menu) {

Menulnflater inflater = getMenuInflater(); //from activity inflater.inflate(R.menu.menu1, menu);

In this code, we first get the MenuInflater from the Activity class and then tell it to inflate the menu XML file into the menu directly.

0 0

Post a comment