I am new to Android. I want to use a checkbox in the menu which has to be checked and unchecked while the user touching. And I need to perform some actions if the check box is checked. Right now I have a checkbox that is checked when the program is run.
I have managed to insert checkbox in the menu and got it to be checked when the app starts, just as it should. But when I try to uncheck it, the program crashes.
Menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/checkBox1"
android:showAsAction="never"
android:title="Allow Check"
android:checkable="true"
android:checked="true" />
<item
android:id="#+id/action_help"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Hai"/>
<item
android:id="#+id/checkBox2"
android:showAsAction="never"
android:title="Allow Check 2"
android:checkable="true"
android:checked="true" />
</menu>
MainActivity.cs
public override bool OnCreateOptionsMenu (IMenu menu)
{
base.OnCreateOptionsMenu (menu);
MenuInflater inflater = this.MenuInflater;
inflater.Inflate (Resource.Menu.menu, menu);
return true;
}
public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
{
switch (item.ItemId) {
case Resource.Id.action_help:
return true;
case Resource.Id.checkBox1:
CheckBox check1 = (CheckBox)FindViewById (Resource.Id.checkBox1);
//Here the check1 come as null, which leads to the crash.
if (check1.Checked == true)
check1.Checked = false;
else
check1.Checked = true;
check1.Click += check1_Click;
return true;
case Resource.Id.checkBox2:
CheckBox check2 = (CheckBox)FindViewById (Resource.Id.checkBox2);
if (check2.Checked == true)
check2.Checked = false;
else
check2.Checked = true;
check2.Click += check2_Click;
return true;
default:
return base.OnOptionsItemSelected (item);
}
}
While trying to retrieve the checkbox declared in the menu, it comes as null. How can I get this working?
Please use code snippet , may this help you
public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
{
switch (item.ItemId) {
case Resource.Id.action_help:
return true;
case Resource.Id.checkBox1:
if (item.IsChecked)
item.SetChecked(false);
else
item.SetChecked(true);
item.Click += check1_Click;
return true;
case Resource.Id.checkBox2:
if (item.IsChecked)
item.SetChecked(false);
else
item.SetChecked(true);
item.Click += check2_Click;
return true;
default:
return base.OnOptionsItemSelected (item);
}
}
Try getting the checked state of the item by calling item.IsChecked .
You have to attach those controls in you OnCreate()
public class MainActivity : Activity
{
private CheckBox check1;
private CheckBox check2;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Create your application here
SetContentView(Resource.Layout.yourLayout);
//find controls in here
check1 = (CheckBox)FindViewById (Resource.Id.checkBox1);
check2 = (CheckBox)FindViewById (Resource.Id.checkBox2);
}
....
and then do your checks
public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
{
switch (item.ItemId) {
case Resource.Id.action_help:
return true;
case Resource.Id.checkBox1:
if (check1.Checked == true)
check1.Checked = false;
else
check1.Checked = true;
check1.Click += check1_Click;
return true;
case Resource.Id.checkBox2:
if (check2.Checked == true)
check2.Checked = false;
else
check2.Checked = true;
check2.Click += check2_Click;
return true;
default:
return base.OnOptionsItemSelected (item);
}
}
Related
So I have a menu with 2 items set on it in res/menu/menu_item.xml. I want to add on onClick method to the Menu Item but where do I put the method? I have the menu_item set on 3 different activities but I want one universal method which is called by the onClick method in the menu_item.xml file.
res/menu/menu_item.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/action_settings"
android:orderInCategory="100"
android:title="Household"
app:showAsAction="never"
android:onClick="myHousehold"/>
<item
android:id="#+id/profile"
android:orderInCategory="100"
android:title="About"
app:showAsAction="never"
android:onClick="aboutApp"/>
</menu>
This is the method which uses this XML to set the menu on each activity.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
You actually don't need the onClick and should add a method like this to enter menu item activities:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_settings:
{
// Here you can set your intent and start the activity
Intent intent1 = new Intent(this, myActivity.class);
this.startActivityForResult(intent1, MY_ACTIVITY_REQUEST);
return super.onOptionsItemSelected(item);
}
case R.id.profile:
{
// Here you can set your intent and start the activity
return super.onOptionsItemSelected(item);
}
default:
return super.onOptionsItemSelected(item);
}
}
You have to overrideonOptionsItemSelected method from your Activity class and check selected item and do your operation as per your needs.
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_action_settings: {
// Do something
return true;
}
case R.id.profile: {
// Do something
return true;
}
}
return super.onOptionsItemSelected(item);
}
Create a common class like below:
//Utils.java
public class Utils{
Context mContext;
// constructor
public Utils(Context context){
this.mContext = context;
}
public String getUserName(){
return "test";
}
public void doSomething(){
// Do something
}
}
Use it from all activities:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_action_settings: {
Utils utils = new Utils(getApplicationContext());
String username = utils.getUserName();
return true;
}
case R.id.profile: {
// Do something
Utils utils = new Utils(getApplicationContext());
utils.doSomething();
return true;
}
}
return super.onOptionsItemSelected(item);
}
Hope this will help~
I have options menu in my toolbar with radibutton item :
<item
android:id="#+id/map_menu"
android:icon="#drawable/ic_layer"
android:orderInCategory="102"
app:showAsAction="always"
android:title="#string/action_settings">
<menu>
<group
android:id="#+id/map_types_group"
android:checkableBehavior="single" >
<item
android:id="#+id/map_terrain"
android:orderInCategory="1"
app:showAsAction="ifRoom"
android:title="#string/map_terrain"/>
<item
android:id="#+id/map_normal"
android:orderInCategory="2"
android:checked="true"
app:showAsAction="ifRoom"
android:title="#string/map_normal"/>
<item
android:id="#+id/map_hybrid"
android:orderInCategory="3"
app:showAsAction="ifRoom"
android:title="#string/map_hybrid"/>
</group>
</menu>
</item>
I want to restore selected radiobutton when orientation change happened in onSaveInstanceState,onRestoreInstanceState but i can't understand how to get selected button from radiogroup in options menu.
Here is a fully working and tested example. With this code in place, no matter how many times you rotate the screen, the currently selected item will persist.
First, create these instance variables to keep track of the state of the menu and have a name for the preference you will be saving in the Bundle:
private final static String MENU_SELECTED = "selected";
private int selected = -1;
MenuItem menuItem;
The saveInstanceState() method should save off the currently selected value:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt(MENU_SELECTED, selected);
super.onSaveInstanceState(savedInstanceState);
}
Update the currently selected item in onOptionsItemSelected():
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Log.d("settings", "id: " + id);
return true;
}
if (id == R.id.map_terrain){
Log.d("menuitem", "terrain id: " + id);
selected = id;
item.setChecked(true);
return true;
}
if (id == R.id.map_normal){
Log.d("menuitem", "normal id: " + id);
selected = id;
item.setChecked(true);
return true;
}
if (id == R.id.map_hybrid){
Log.d("menuitem", "hybrid id: " + id);
selected = id;
item.setChecked(true);
return true;
}
return super.onOptionsItemSelected(item);
}
In onCreate(), load the saved data if it exists:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null){
selected = savedInstanceState.getInt(MENU_SELECTED);
}
}
And then re-select the previously selected item in onCreateOptionsMenu():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if (selected == -1){
return true;
}
switch (selected){
case R.id.map_terrain:
menuItem = (MenuItem) menu.findItem(R.id.map_terrain);
menuItem.setChecked(true);
break;
case R.id.map_normal:
menuItem = (MenuItem) menu.findItem(R.id.map_normal);
menuItem.setChecked(true);
break;
case R.id.map_hybrid:
menuItem = (MenuItem) menu.findItem(R.id.map_hybrid);
menuItem.setChecked(true);
break;
}
return true;
}
MenuItem in onOptionsItemSelected has item.isChecked() method, just use it. You can store one boolean field(it wouldn't be a bad thing in my opinion) and change it whenever change in radiog group occurs
Then you can have your id by simply calling:
if(item.isChecked()) {
your_id_field = item.getItemId()
}
Create a variable eg: menu_selection to store the id of the menu item selected. Initially you can define 0
private int menu_selection = 0;
Save and restore the value of the variable using onSaveInstanceState and onRestoreInstanceState
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the id of radio button selected in the menu
savedInstanceState.putInt("selection", menu_selection);
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
menu_selection = savedInstanceState.getInt("selection");
}
In onOptionsItemSelected , assign the id of the menu item selected item.getItemId() to the variable
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if (item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
menu_selection = item.getItemId();
}
switch (item.getItemId()) {
case R.id.map_terrain:
//some action here
return true;
case R.id.map_normal:
//some action here
return true;
case R.id.map_hybrid:
//some action here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In onCreateOptionsMenu , find the menu item identified with the value in the variable and check it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
if (menu_selection != 0) {
MenuItem selected = (MenuItem) menu.findItem(menu_selection);
selected.setChecked(true);
}
return true;
}
I want to create an intent that starts a new activity once a Menu Item is clicked, but I'm not sure how to do this. I've been reading through the android documentation, but my implementation isn't correct..and some guidance in the right direction would help. I've listed my code below and commented out my problem areas, I think I'm invoking the wrong method.
package com.jbsoft.SimpleFlashlight;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SimpleFlashLightActivity extends Activity {
Button GreenButton; // Declare instances of buttons to use later
Button BlueButton;
private static final int OK_MENU_ITEM = Menu.FIRST;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BlueButton = (Button) findViewById(R.id.bluebutton);
BlueButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Display msg when user clicks Blue Button
showColorChangeMsg();
// Switch Activities on click
Intent blueintent = new Intent(SimpleFlashLightActivity.this,
BlueFlashLightActivity.class);
startActivity(blueintent);
}
});
//Install listener for second button
GreenButton = (Button) findViewById(R.id.greenbutton);
GreenButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Display msg when user clicks Green Button
showColorChangeMsg();
Intent greenintent = new Intent(SimpleFlashLightActivity.this,
GreenFlashLightActivty.class);
startActivity(greenintent);
}
});
;
/**************************************************************************************/
// Method Declarations // THIS IS WHERE I'M HAVING A PROBLEM
MenuItem AddColorButton = (MenuItem)findViewById(R.id.menu_insert);
boolean onOptionsItemSelected(AddColorButton) {
Intent intent = new Intent(SimpleFlashLightActivity.this,
BlueFlashLightActivity.class);
startActivity(intent);
return true;
;
};
/****************************************************************************************/
}
private void showColorChangeMsg()
{
Toast msgtoast = Toast.makeText(this.getBaseContext(), "SWITCH COLOR!",
Toast.LENGTH_LONG);
msgtoast.show();
}
private void showMsg(String msg) {
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.show();
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case OK_MENU_ITEM:
showMsg("OK");
break;
}
return super.onOptionsItemSelected(item);
}
}
Add Following Code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_item:
Intent i = new Intent(this,SecondActivity.class);
this.startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Menu items file looks like
res/menu/menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/settings"
android:title="Setting"
app:showAsAction="never" />
<item
android:id="#+id/my_activity"
android:title="My Activity"
app:showAsAction="always"
android:icon="#android:drawable/btn_radio"/>
</menu>
Java code looks like
src/MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.my_activity) {
Intent intent1 = new Intent(this,MyActivity.class);
this.startActivity(intent1);
return true;
}
if (id == R.id.settings) {
Toast.makeText(this, "Setting", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
And add following code to your AndroidManifest.xml file
<activity
android:name=".MyActivity"
android:label="#string/app_name" >
</activity>
I hope it will help you.
in addition to the options shown in your question, there is the possibility of implementing the action directly in your xml file from the menu, for example:
<item
android:id="#+id/OK_MENU_ITEM"
android:onClick="showMsgDirectMenuXml" />
And for your Java (Activity) file, you need to implement a public method with a single parameter of type MenuItem, for example:
private void showMsgDirectMenuXml(MenuItem item) {
Toast toast = Toast.makeText(this, "OK", Toast.LENGTH_LONG);
toast.show();
}
NOTE: This method will have behavior similar to the onOptionsItemSelected (MenuItem item)
This code is work for me
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// add your action here that you want
return true;
}
else if (id==R.id.login)
{
// add your action here that you want
}
return super.onOptionsItemSelected(item);
}
This is how it looks like in Kotlin
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_logout"
android:orderInCategory="101"
android:title="#string/sign_out"
app:showAsAction="never" />
Then in MainActivity
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
This is onOptionsItemSelected function
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when(item.itemId){
R.id.action_settings -> {
true
}
R.id.action_logout -> {
signOut()
true
}
else -> return super.onOptionsItemSelected(item)
}
}
For starting new activity
private fun signOut(){
MySharedPreferences.clearToken()
startSplashScreenActivity()
}
private fun startSplashScreenActivity(){
val intent = Intent(GrepToDo.applicationContext(), SplashScreenActivity::class.java)
startActivity(intent)
finish()
}
Replace Your onOptionsItemSelected as:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case OK_MENU_ITEM:
startActivity(new Intent(DashboardActivity.this, SettingActivity.class));
break;
// You can handle other cases Here.
default:
super.onOptionsItemSelected(item);
}
}
Here, I want to navigate from DashboardActivity to SettingActivity.
Im trying to do something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//multiListener = false;
menu.add(0,START_DELETE,0, "Delete selected..").setEnabled(multiListener);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.multiselect:
if(multiselect == 0) { multiselect = 1;
multiListener = true;
Log.d("DH", "index="+multiListener);
}
else if(multiselect == 1) { multiselect = 0; multiListener = false;
Log.d("DH", "index="+multiListener);
}
fillData();
return true;
case START_DELETE:
Toast.makeText(Notepadv3.this, "Pressed delete", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Basically, if multiListener = true; make "Delete selected.." pressable otherwise gray it out...
This simple , thing.. doesn't want to work out with me,
for somehow... the button is always greyed out, although Log says that, it changes to true...
Anyone, know something?
You should call setEnabled() again to change item's state. Its state doesn't bind to a variable. This method should be called in onPrepareOptionsMenu().
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,
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
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);
}
}
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.
#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);
}
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);
}
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;
}
}
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
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.
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.
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.
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