Monday, November 18, 2024
Google search engine
HomeLanguagesJavaSub-Menu in Android

Sub-Menu in Android

In android, Menu is an important part of the UI component which is used to provide some common functionality around the application. With the help of Menu, users can experience a smooth and consistent experience throughout the application. In order to use the Menu, we should define it in a separate XML file and use that file in our application based on our requirements. Also, we can use menu APIs to represent user actions and other options in our android application activities.

What is Sub-Menu ?

Sub menu is basically list of menus inside a menu option. So, we can understand sub-menu as nested menus. So, in this article we are going to create sub-menu . For creating sub-menu , we are going to create and android application in android studio. A sample video is given below to get an idea about what we are going to do in this article.

Note: This Android article covered in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<!-- main page on which sub-menu will be shown-->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".MainActivity">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        tools:ignore="MissingConstraints">
        
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:textColor="@color/purple_700"
            android:text="Sub-Menu Example on GEEKSFORGEEKS" />
  
    </LinearLayout>
    
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Working with the Menu File

Create a new android resource file(XML) and name it Menu, and add the below code to it. 

XML




<?xml version="1.0" encoding="UTF-8"?>
<!-- Menu file  -->
   <item android:id="@+id/i1" android:title="sub-menu1" HTMLCode:showAsAction="never">
      <menu>
         <item android:id="@+id/s11" android:title="sub-menu-item1" HTMLCode:showAsAction="ifRoom|withText" />
         <item android:id="@+id/s12" android:title="sub-menu-item-2" HTMLCode:showAsAction="ifRoom|withText" />
         <item android:id="@+id/s13" android:title="sub-menu-item-3" HTMLCode:showAsAction="ifRoom|withText" />
      </menu>
   </item>
   <item android:id="@+id/i2" android:title="sub-menu2" HTMLCode:showAsAction="never">
      <menu>
         <item android:id="@+id/s21" android:title="sub-menu-item-4" HTMLCode:showAsAction="ifRoom|withText" />
         <item android:id="@+id/s22" android:title="sub-menu-item-5" HTMLCode:showAsAction="ifRoom|withText" />
      </menu>
   </item>
</menu>


Step 4: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail. 

Java




package com.example.gfgfirstapp;
  
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
  
public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // for change the background color of title bar
        ActionBar aBar; aBar= getSupportActionBar();
        ColorDrawable cd = new ColorDrawable(Color.parseColor("#FF00FF00"));
        aBar.setBackgroundDrawable(cd);
    }
      
    // using inflater for menu
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.sub_menu, menu);
        return true;
    }
  
}


Kotlin




package com.example.gfgfirstapp;
  
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
  
class MainActivity : AppCompatActivity() {
  
  override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
          
        // for changing the background color of title bar
        val aBar = supportActionBar
        val cd = ColorDrawable(Color.parseColor("#FF00FF00"))
        aBar?.setBackgroundDrawable(cd)
          
    }
    
    // using inflater for menu
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.sub_menu, menu)
        return true
    }
     
    
}


Output:

RELATED ARTICLES

Most Popular

Recent Comments