Android, How to create option Menu

AndroidAndroid Menu

Android Problem Overview


Here I tried to make option menu, but menu is not displaying on screen, so please guide me where am I doing mistake...

MenuTest.java

public class MenuTest extends Activity {
   @Override
public boolean onCreateOptionsMenu(Menu menu) {
	MenuInflater inflater=getMenuInflater();
	inflater.inflate(R.menu.more_tab_menu, menu);
	return super.onCreateOptionsMenu(menu);
	
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch(item.getItemId())
	{
	case R.id.feeds:
		break;
	case R.id.friends:
		break;
	case R.id.about:
		break;
	}
	return true;
}
}

And my XML file is more_tab_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
	android:id="@+id/feeds"
	android:title="Feeds"/>
<item
	android:id="@+id/friends"
	android:title="Friends"/>
<item
	android:id="@+id/about"
	android:title="About"/>
</menu>

Please guide me,

Android Solutions


Solution 1 - Android

public class MenuTest extends Activity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.more_tab_menu, menu);
        
        // return true so that the menu pop up is opened
        return true; 
    }
}

and don't forget to press the menu button or icon on Emulator or device

Solution 2 - Android

please see :==

private int group1Id = 1;

int homeId = Menu.FIRST;
int profileId = Menu.FIRST +1;
int searchId = Menu.FIRST +2;
int dealsId = Menu.FIRST +3;
int helpId = Menu.FIRST +4;
int contactusId = Menu.FIRST +5;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(group1Id, homeId, homeId, "").setIcon(R.drawable.home_menu);
    menu.add(group1Id, profileId, profileId, "").setIcon(R.drawable.profile_menu);
    menu.add(group1Id, searchId, searchId, "").setIcon(R.drawable.search_menu);
    menu.add(group1Id, dealsId, dealsId, "").setIcon(R.drawable.deals_menu);
    menu.add(group1Id, helpId, helpId, "").setIcon(R.drawable.help_menu);
    menu.add(group1Id, contactusId, contactusId, "").setIcon(R.drawable.contactus_menu);

    return super.onCreateOptionsMenu(menu);	
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
    case 1:
        // write your code here
        Toast msg = Toast.makeText(MainHomeScreen.this, "Menu 1", Toast.LENGTH_LONG);
	    msg.show();
	    return true;
		
    case 2:
	    // write your code here
	    return true;
		
    case 3:
        // write your code here
	    return true;
		
	case 4:
	    // write your code here
	    return true;
		
    case 5:
	    // write your code here
	    return true;
		
    case 6:
	    // write your code here
	    return true;
		
    default:
	    return super.onOptionsItemSelected(item);
    }
}

Solution 3 - Android

Change your onCreateOptionsMenu method to return true. To quote the docs:

> You must return true for the menu to be displayed; if you return false it will not be shown.

Solution 4 - Android

@Override
public boolean onCreateOptionsMenu(Menu menu) {
	new MenuInflater(this).inflate(R.menu.folderview_options, menu);
	return (super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

	if (item.getItemId() == R.id.locationListRefreshLocations) {
		Cursor temp = helper.getEmployee(active_employeeId);
		String[] matches = new String[1];
		if (temp.moveToFirst()) {
			matches[0] = helper.getEmployerID(temp);
		}
		temp.close();				
		startRosterReceiveBackgroundTask(matches);
	} else if (item.getItemId()==R.id.locationListPrefs) {
		startActivity(new Intent(this, PreferencesUnlockScreen.class));
		return true;
	}			
	return super.onOptionsItemSelected(item);
}	

Solution 5 - Android

you can create options menu like below:

Menu XML code:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/Menu_AboutUs"
        android:icon="@drawable/ic_about_us_over_black"
        android:title="About US"/>
    <item
        android:id="@+id/Menu_LogOutMenu"
        android:icon="@drawable/ic_arrow_forward_black"
        android:title="Logout"/>
</menu>

How you can get the menu from MENU XML(Convert menu XML to java):

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_options_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

How to get Selected Item from Menu:

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){

            case R.id.Menu_AboutUs:
                //About US
                break;

            case R.id.Menu_LogOutMenu:
                //Do Logout
                break;
        }
        return super.onOptionsItemSelected(item);
    }

Solution 6 - Android

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class AndroidWalkthroughApp2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	// show menu when menu button is pressed
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.options_menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// display a message when a button was pressed
    	String message = "";
    	if (item.getItemId() == R.id.option1) {
    		message = "You selected option 1!";
    	}
    	else if (item.getItemId() == R.id.option2) {
    		message = "You selected option 2!";
    	}
    	else {
    		message = "Why would you select that!?";
    	}
    	
    	// show message via toast
    	Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
    	toast.show();
    	
    	return true;
    }
}

Solution 7 - Android

Replace return super.onCreateOptionsMenu(menu); with return true; in your onCreateOptionsMenu method This will help

And you should also have the onCreate method in your activity

Solution 8 - Android

The previous answers have covered the traditional menu used in android. Their is another option you can use if you are looking for an alternative

https://github.com/AnshulBansal/Android-Pulley-Menu

Pulley menu is an alternate to the traditional Menu which allows user to select any option for an activity intuitively. The menu is revealed by dragging the screen downwards and in that gesture user can also select any of the options.

Solution 9 - Android

Android UI programming is a little bit tricky. To enable the Options menu, in addition to the code you wrote, we also need to call setHasOptionsMenu(true) in your overriden method OnCreate(). Hope this will help you out.

Solution 10 - Android

IF your Device is running Android v.4.1.2 or before,
the menu is not displayed in the action-bar.
But it can be accessed through the Menu-(hardware)-Button.

Solution 11 - Android

Good Day I was checked And if You choose Empty Activity You Don't have build in Menu functions For Build in You must choose Basic Activity In this way You Activity will run onCreateOptionsMenu

Or if You work in Empty Activity from start Chenge in styles.xml the

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVishesh ChandraView Question on Stackoverflow
Solution 1 - AndroidDEVANG SHARMAView Answer on Stackoverflow
Solution 2 - AndroidPatel EktaView Answer on Stackoverflow
Solution 3 - AndroidFelixView Answer on Stackoverflow
Solution 4 - AndroidMayank SainiView Answer on Stackoverflow
Solution 5 - AndroidDevenView Answer on Stackoverflow
Solution 6 - AndroidDurul DalkanatView Answer on Stackoverflow
Solution 7 - AndroidReNaView Answer on Stackoverflow
Solution 8 - AndroidAnshul BansalView Answer on Stackoverflow
Solution 9 - AndroidanhldbkView Answer on Stackoverflow
Solution 10 - AndroidComputerkasteView Answer on Stackoverflow
Solution 11 - AndroidMichaelView Answer on Stackoverflow