Skip to content
Related Articles

Related Articles

How to Add a Custom View Inside ActionBar in Android?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 14 Feb, 2022

In Android, an Action Bar, or an app bar is a dedicated space at the top of the application on the screen that indicates the name of the application or activity. An Action bar helps users navigate or locate themself in the application. A sample Action Bar is shown in the below image.

ActionBar in Android

In this article, we will show you how you could customize the Action Bar by creating and displaying a Custom View in Android. 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. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Create a Custom View my_layout.xml

Navigate to app > res > layout and create a new Android Resource Layout by right-clicking on the folder and selecting the appropriate option. Create the desired view. We have created one shown below. We have created a horizontal Linear Layout which is equally shared by a TextView and a Button.


<?xml version="1.0" encoding="utf-8"?>

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. No changes are required as this file holds no significance in changing Action Bar attributes.


<?xml version="1.0" encoding="utf-8"?>
        android:text="Example of a Custom Action Bar!"
        app:layout_constraintTop_toTopOf="parent" />

Step 4: 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.


package org.geeksforgeeks.customactionbar
import android.os.Bundle
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Calling the support action bar and setting it to custom
        this.supportActionBar!!.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
        // Displaying the custom layout in the ActionBar


You can see below that the custom layout that we created is displayed in the Action Bar.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!