Friday, 6 January 2017

Option Menu in Android





Step 1 : Create menu folder in res folder


Step 2 : Create menu.xml in menu folder

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   
<item android:id="@+id/new_game"
         
android:icon="@drawable/ic_new_game"
         
android:title="@string/new_game"
         
android:showAsAction="ifRoom"/>
   
<item android:id="@+id/help"
         
android:icon="@drawable/ic_help"
         
android:title="@string/help" />
</menu>

Step 2 : Add this two function in java file

@Override

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    
    switch (item.getItemId()) {
        case R.id.new_game:
            //put your code here
            return true;

        case R.id.help:
            //put your code here
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

No comments:

Post a Comment