How to show/hide a menu button in Android dynamically:
In this post, we will learn how to show/hide a menu button in Android programmatically in Kotlin. We will create one new Android Studio project and check that with an example.
Create a new Android Studio project:
Create a new Android studio project with an empty Activity. We will add one button in this activity. On click of this button, we will show/hide the menu button :
For this example, MainActivity.kt is the main activity and activity_main.xml is the xml file for that activity. Below is the code for MainActivity:
package com.codevscolor.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun toggleMenuVisibility(view: View) {}
}
and belos is for activity_main :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me !"
android:onClick="toggleMenuVisibility"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
We have one button at the center of the screen and it calls toggleMenuVisibility method if we click on it.
Create one delete icon and menu :
- Right click on the project name -> New -> Vector Asset.
Select one delete icon as the clip art, change the color to ffffff click on Next and click on Finish. It will create one icon in the asset folder that we can add to the menu.
- To create a menu, right click on the project, New -> Android Resource File. Set the file name as main_menu, change the resource type to Menu.
It will create one new menu main_menu.xml file. Update this file as below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/app_bar_search"
android:icon="@drawable/ic_baseline_delete_24"
android:title="delete"
app:showAsAction="always"
/>
</menu>
Here, we have added one delete icon as the menu icon. Let’s write the code to show/hide this icon in the activity file:
Change MainActivity.kt :
Change the MainActivity.kt file as below:
package com.codevscolor.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.View
class MainActivity : AppCompatActivity() {
private var mainMenu: Menu? = null
private var menuShowing = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
mainMenu = menu
return super.onCreateOptionsMenu(menu)
}
fun toggleMenuVisibility(view: View) {
mainMenu?.findItem(R.id.menu_delete)?.isVisible = !menuShowing
menuShowing = !menuShowing
}
}
Here,
- In onCreateOptionMenu, we are inflating the menu main_menu. Also, we are storing the menu in the variable mainMenu for future.
- In toggleMenuVisibility, we are finding the menu item menu_delete and changing the visibility of this button.
You will see one screen as like below:
Clicking on the button will show/hide the delete menu item.
You might also like:
- Create a recyclerview with image from API in Kotlin Android
- How to delete an item from a recyclerview in Android(Kotlin)
- How to add pull to refresh/ swipe refresh in an Android project in Kotlin
- How to change the App name and package name in Android Studio
- Adapter callback in Android using lambda function of Kotlin