Friday, 11 September 2015

creating simple menu

Create a xml file of type menu and that will go into menu folder

put whatever option u want as <item > inside the xml

<item android:id="@+id/item2" android:title="option2"  android:icon="@android:drawable/ic_menu_call" >
</item>
 
 then we will have to modify the Oncreatemenu thing
 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                   super.onCreateContextMenu(menu, v, menuInfo);

    MenuInflater menuInflater= getMenuInflater();
    menuInflater.inflate(R.menu.contextm,menu);
}
 
use inflater to inflate it with the xml file the we created
 
this just does the work to create the option and menu thing
now, to control the clicks we onOptionsSelected
 
 
 
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will    // automatically handle clicks on the Home/Up button, so long    // as you specify a parent activity in AndroidManifest.xml.    int id = item.getItemId();

    //noinspection SimplifiableIfStatement    if (id == R.id.action_settings) {
        return true;
//can also do a logcat here
    }

    return super.onOptionsItemSelected(item);
} 

No comments:

Post a Comment