home top ad

How to create menu and sub menu in android studio

Hello friends today we will learn that how we will create option menu or sub menu in android studio.
If you do not know how to create option menu, then see this.
We use sub menu very often when we have to show category in menu in our app.Just like we have to show category in our app, we will create sub menu of the same type in itself.

To create sub menu, we have to first create a new project or paste it in the app in which we want to create.After creating the project, When we will create the project we will see the xml file and a java file MainActivity. But we'll need a sub menu for that .we will create a menu folder by creating right on the res folder.And after that, by right clicking on the menu folder, we will create a menu xml file.You can give whatever name you likeAll these layout and java code is given with below name.


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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        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>
We do not need this layout to create all the Submenus.

MainActivity.java
package com.example.optionmenuandsubmenu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

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

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId())
        {
            case R.id.item1:
                Toast.makeText(this, "item1 ....", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.item2:
                Toast.makeText(this, "item2 ....", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.item3:
                Toast.makeText(this, "item3 ....", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.item4:
                Toast.makeText(this, "item4 ....", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.item5:
                Toast.makeText(this, "item5 ....", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.item6:
                Toast.makeText(this, "item6 ....", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return  false;
        }
    }
}
Here we will override two methods onCreateOptionMenu and onOptionItemSelectedIn onCreateOptionmenu we use inflater to R. menu. xml and write the parameter of last menu type. And in the last we print a toast by id of each item in onOptionItem Selected.

popup_menu
<?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/item1"
        android:title="Menu 1"
        android:icon="@drawable/ic_share"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/item2"
        android:title="Menu 2"
        android:icon="@drawable/ic_privacy"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/item3"
        android:title="Menu 3"
        android:icon="@drawable/ic_privacy"     />

    <item android:id="@+id/item4"
        android:title="Menu 4"
        android:icon="@drawable/ic_privacy" >

        <menu>

            <item android:id="@+id/item5"
                android:title="Menu 5"
                android:icon="@drawable/ic_privacy"     />

            <item android:id="@+id/item6"
                android:title="Menu 6"
                android:icon="@drawable/ic_privacy"     />

        </menu>

    </item>



</menu>
If you are getting icon error then you can create icon by right clicking on drawable folder and going to vector asset. I hope you like this article.
Thank you

Post a Comment

0 Comments