0% found this document useful (0 votes)
233 views

How Do I Hide and Show A Menu Item in The Android ActionBar

This document provides steps to hide and show a menu item in the Android ActionBar. It involves creating an Android project with a toggle button in the layout. The toggle button will listen for checked changes and call methods to set the visibility and enabled status of a menu item. The menu item is defined in an XML menu resource. When the activity is created, the menu is inflated and the reference is stored to call the visibility methods on check changes.

Uploaded by

Hawaz Beyene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
233 views

How Do I Hide and Show A Menu Item in The Android ActionBar

This document provides steps to hide and show a menu item in the Android ActionBar. It involves creating an Android project with a toggle button in the layout. The toggle button will listen for checked changes and call methods to set the visibility and enabled status of a menu item. The menu item is defined in an XML menu resource. When the activity is created, the menu is inflated and the reference is stored to call the visibility methods on check changes.

Uploaded by

Hawaz Beyene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

8/8/2020 How do I hide and show a menu item in the Android ActionBar?

How do I hide and show a menu item in the Android ActionBar?

This example demonstrates how do I hide and show a menu item in the Android ActionBar.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details
to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=
xmlns:tools
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ToggleButton
android:id="@+id/btnMenuItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Step 3 − Add the following code to src/MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
ToggleButton button1;
Menu myMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.btnMenuItem);
button1.setOnCheckedChangeListener(onCheckedChangeListener);
}
CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButto
CompoundButt
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
https://www.tutorialspoint.com/how-do-i-hide-and-show-a-menu-item-in-the-android-actionbar 1/4
8/8/2020 How do I hide and show a menu item in the Android ActionBar?
if(myMenu != null) {
if (buttonView == button1) {
myMenu.findItem(R.id.menu_action_share).setVisible(isChecked);
myMenu.findItem(R.id.menu_action_share).setEnabled(isChecked);
}
}
}
};
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menuitem, menu);
myMenu = menu;
MenuItem item = menu.findItem(R.id.menu_action_share);
if (item != null) {
item.setVisible(true);
}
return true;
}
}

Step 4 − Right click on res, create menu folder. Right click on the menu folder, create a menu
resource file (menuitem.xml) and add the following code −

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://s
<item
android:id="@+id/menu_action_share"
android:icon="@drawable/ic_share_black_24dp"
android:title="Share"
app:showAsAction="ifRoom"/>
</menu>

Step 5 − Add the following code to androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com
"app.co
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
https://www.tutorialspoint.com/how-do-i-hide-and-show-a-menu-item-in-the-android-actionbar 2/4
8/8/2020 How do I hide and show a menu item in the Android ActionBar?

<action android:name="android.intent.action.MAIN" />


<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device
with your computer. To run the app from android studio, open one of your project's activity files and
click Run icon from the toolbar. Select your mobile device as an option and then check your
mobile device which will display your default screen −

https://www.tutorialspoint.com/how-do-i-hide-and-show-a-menu-item-in-the-android-actionbar 3/4
8/8/2020 How do I hide and show a menu item in the Android ActionBar?

Click here to download the project code

https://www.tutorialspoint.com/how-do-i-hide-and-show-a-menu-item-in-the-android-actionbar 4/4

You might also like