Open In App

Create Options Menu in ActionBar in Android using Jetpack Compose

Last Updated : 30 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Android, ac ActionBar or a TopBar is a UI element that is present at the top of the activity screen. An ActionBar by default displays the activity name inside it. However, we can add other elements like the back button, images, options menu, etc inside an ActionBar. So in this article, we will show you how you could Create Options Menu in ActionBar in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

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. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jcactionbarmenuoptions
  
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // Calling the composable function
            // to display element and its contents
            MainContent()
        }
    }
}
  
// Creating a composable function 
// to display Top Bar and options menu
@Composable
fun MainContent() {
    
    // Create a boolean variable 
    // to store the display menu state
    var mDisplayMenu by remember { mutableStateOf(false) }
      
    // fetching local context
    val mContext = LocalContext.current
  
    // Creating a Top bar
    TopAppBar(
        title = { Text("GFG | Menu Options", color = Color.White) } ,backgroundColor = Color(0xff0f9d58),
        actions = {
              
            // Creating Icon button favorites, on click 
            // would create a Toast message
            IconButton(onClick = { Toast.makeText(mContext, "Favorite", Toast.LENGTH_SHORT).show() }) {
                Icon(Icons.Default.Favorite, "")
            }
  
            // Creating Icon button for dropdown menu
            IconButton(onClick = { mDisplayMenu = !mDisplayMenu }) {
                Icon(Icons.Default.MoreVert, "")
            }
  
            // Creating a dropdown menu
            DropdownMenu(
                expanded = mDisplayMenu,
                onDismissRequest = { mDisplayMenu = false }
            ) {
  
                // Creating dropdown menu item, on click
                // would create a Toast message
                DropdownMenuItem(onClick = { Toast.makeText(mContext, "Settings", Toast.LENGTH_SHORT).show() }) {
                    Text(text = "Settings")
                }
  
                // Creating dropdown menu item, on click 
                // would create a Toast message
                DropdownMenuItem(onClick = { Toast.makeText(mContext, "Logout", Toast.LENGTH_SHORT).show() }) {
                    Text(text = "Logout")
                }
            }
        }
    )
}
  
// For displaying preview in 
// the Android Studio IDE emulator
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MainContent()
}


Output:

You can see that we are able to create options menu as well as a drop-down menu as seen in the below output video.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads