Create runtime menu and add new or remove menu dynamically in Android Studio(Java)

 Create runtime menu and 
add new or remove menu dynamically

Here is an example of the Create runtime menu and add new or remove menu dynamically



Step 1: Create a new project in Android Studio.

Step 2: Create a new file in the res/layout directory called activity_main.xml

<?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">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to add new or remove menu item\n at runtime"
        android:textStyle="bold"
        android:gravity="center"
        android:textSize="20sp"
        android:onClick="toggleMenu"
        android:clickable="true"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Create a new java file called MainActivity.java

Step 3-a: Open MainActivity.java and add the following three variables just below the class declaration

private final int MENU_DOWNLOAD = 1;
private final int MENU_SETTINGS = 2;
private boolean showDownloadMenu = false;


Step 3-b: Add the following method to handle the click.

public void toggleMenu(View view) {
    showDownloadMenu=!showDownloadMenu;
}


Step 3-c:Override onCreateOptionsMenu() to create the menu. Here is the code to dynamically build the menu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, MENU_DOWNLOAD, 0, "Download");
    menu.add(0, MENU_SETTINGS, 0, "Setting");
    return true;
}


Step 3-d: For best programming practice, Override onPrepareOptionsMenu() to update or change your                        menu. Here is the code to change the visibility of the Download menu item based on our flag.

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem menuItem = menu.findItem(MENU_DOWNLOAD);
    menuItem.setVisible(showDownloadMenu);
    return true;
}

Step 3-e: Override onOptionsItemSelected() code shows how to respond to each menu item

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case MENU_DOWNLOAD:
            Toast.makeText(this, "Click download",
                        Toast.LENGTH_LONG).show();
            break;
        case MENU_SETTINGS:
            Toast.makeText(this, "Click Setting",
                        Toast.LENGTH_LONG).show();
            break;
        default:
            return super.onContextItemSelected(item);
    }
    return true;
}


Step 3-f: Final code of MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private final int MENU_DOWNLOAD = 1;
    private final int MENU_SETTINGS = 2;
    private boolean showDownloadMenu = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //handle click
    public void toggleMenu(View view) {
        showDownloadMenu=!showDownloadMenu;
    }
    //Android calls onCreateOptionsMenu() to create the menu.
    //Here is the code to dynamically build the menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_DOWNLOAD, 0, "Download");
        menu.add(0, MENU_SETTINGS, 0, "Setting");
        return true;
    }
    //For best programming practice, don't use onCreateOptionsMenu()
    //to update or change your menu; instead, use onPrepareOptionsMenu()
    //Here is the code to change the visibility of the Download menu item based on our flag
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.findItem(MENU_DOWNLOAD);
        menuItem.setVisible(showDownloadMenu);
        return true;
    }
    //onOptionsItemSelected() code shows how to respond to each menu item
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_DOWNLOAD:
                Toast.makeText(this, "Click download",
                        Toast.LENGTH_LONG).show();
                break;
            case MENU_SETTINGS:
                Toast.makeText(this, "Click Setting",
                        Toast.LENGTH_LONG).show();
                break;
            default:
                return super.onContextItemSelected(item);
        }
        return true;
    }
}








Post a Comment

0 Comments