0% found this document useful (0 votes)
6 views13 pages

Customizing ActionBar in Android

The document provides an overview of the ActionBar in Android, detailing its purpose, components, and customization options for developers. It includes a step-by-step guide on creating a custom ActionBar in an Android application, along with code examples for implementation. Additionally, it discusses the advantages and disadvantages of using ActionBar, noting the introduction of ToolBar for resolving some limitations.

Uploaded by

shoukatnimra9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Customizing ActionBar in Android

The document provides an overview of the ActionBar in Android, detailing its purpose, components, and customization options for developers. It includes a step-by-step guide on creating a custom ActionBar in an Android application, along with code examples for implementation. Additionally, it discusses the advantages and disadvantages of using ActionBar, noting the introduction of ToolBar for resolving some limitations.

Uploaded by

shoukatnimra9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

Search... Sign In

Java Android Kotlin Flutter Dart Android with Java Android Studio Android Projects And

ActionBar in Android with Example


Last Updated : 23 Jul, 2025

In Android applications, ActionBar is the element present at the top of


the activity screen. It is a salient feature of a mobile application that has a
consistent presence over all its activities. It provides a visual structure to
the app and contains some of the frequently used elements for the users.
Android ActionBar was launched by Google in 2013 with the release of
Android 3.0(API 11). Before that, the name of this top most visual
element was AppBar. AppBar contains only the name of the application
or current activity. It was not very much useful for the users and
developers also have negligible option to customize it.

Google announced a support library along with the introduction of


ActionBar. This library is a part of AppCompat and its purpose is to
provide backward compatibility for older versions of Android and to
support tabbed interfaces. All applications that use the default theme
provided by the Android([Link]),
contains an ActionBar by default. However, developers can customize it
in several ways depending upon their needs.

Components included in the ActionBar are:

App Icon: Display the branding logo/icon of the application.


View Controls: Section that displays the name of the application or
current activity. Developers can also include spinner or tabbed
navigation for switching between views.
Action Button: Contains some important actions/elements of the app
that may be required to the users frequently.
Action Overflow: Include other actions that will be displayed as a
menu.
[Link] 1/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

Designing a Custom ActionBar


The following example demonstrates the steps involved in creating a
custom Action Bar for the MainActivity of an application. All important
aspects of visual elements like icon, title, subtitle, action buttons, and
overflow menu will be covered.

Note: Following steps are performed on Android Studio Ladybug


2024.2.2

Step 1: Enable Action Bar

In the new versions of Android, the Action bar is disabled explicitly by


default. To enable, navigate to app > res > values > [Link].
Remove the "NoActionBar" text from the parent style. Follow the below
code, to understand clearly.

Before

<style name="[Link]"
parent="[Link]">

After

<style name="[Link]" parent="[Link]">

Output:

[Link] 2/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

Step 2: Creating a new directory and design items of ActionBar

To code the elements of ActionBar, create a new directory in the resource


folder of the application project files. Right-click on the res folder and
selects New > Android Resource Directory. Give the name "menu" to
the new directory and resource type.

[Link] 3/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

Further, create a new Menu Resource File by right click on the menu
directory. As the ActionBar is being created for the main Activity, type the
name as "main" to the Menu Resource File. With this, a new file named
"[Link]" must be created under the menu directory. In this file, one
can declare the items which will be displayed as the action buttons of
the ActionBar.

For every menu items, the following attributes are needed to be


configured:

android:title: Its value contains the title of the menu item that will be
displayed when a user clicks and holds that item in the app.
android:id: A unique ID for the menu item that will be used to access
it anywhere in the whole application files.
android:orderInCategory: The value of this attribute specify the item's
position in the ActionBar. There are two ways to define the position of
different menu items. The first one is to provide the same value of this
attribute for all items and the position will be defined in the same
order as they are declared in the code. The second way is to provide a
different numeric value for all items and then the items will position
themselves according to ascending order of this attribute's value.

[Link] 4/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

app:showAsAction: This attribute defines how the item is going to be


present in the action bar. There are four possible flags to choose from:
always: To display the item in the ActionBar all the time.
ifRoom: To keep the item if space is available.
never: With this flag, the item will be not be displayed as an icon
in ActionBar, but will be present in the overflow menu.
withText: To represent an item as both icon and the title, one can
append this flag with the always or ifRoom flag(always|withText
or ifRoom|withText).
android:icon: The icon of an item is referenced in the drawable
directories through this attribute.

Icon of an ActionBar Item

In order to provide an icon to an item, right-click on the res folder, select


new, and then Image Asset. A dialog box will appear, choose the Icon
Type as Action Bar and Tab Icons. Choose assets type as "Clip Art" and
select an image from the clip art collection. Provide a desired name to the
icon. Click on Next, then Finish. This icon will now get loaded in the
drawable directory of the res folder. The name provided by the
developers to these icons will now be used to reference the item's icon
resource.

[Link] 5/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

Below is the code to place a search icon, refresh icon, and an overflow
menu in the ActionBar.

[Link]:

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


<menu xmlns:android="[Link]
xmlns:app="[Link]
<!-- action button for search -->
<item
android:id="@+id/search"
android:icon="@android:drawable/ic_menu_search"
android:orderInCategory="100"
android:title="search"
app:showAsAction="ifRoom" />

<!-- action button for call -->


<item
android:id="@+id/call"
android:icon="@android:drawable/ic_menu_call"
android:orderInCategory="100"
android:title="call"
app:showAsAction="ifRoom" />

<!-- action button for copy -->


<item
android:id="@+id/copy"
android:orderInCategory="100"
android:title="copy"
app:showAsAction="never" />
</menu>

Design UI:

[Link] 6/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

Step 3: Working with the Activity File

The items of an ActionBar is designed with a purpose to perform some


operations. Those operations/actions of the items are declared in that
Activity file for which the ActionBar has been designed. In this example,
the target activity is the MainActivity file. Further, the custom title,
subtitle, and application logo are also defined in this file. Below is the
proper code to design all mentioned items and to display a toast
message when a user clicks on the items of ActionBar.

MainActivity File:

Java Kotlin

package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)

// calling this activity's function to


// use ActionBar utility methods
val actionBar = supportActionBar

// providing title for the ActionBar


actionBar!!.title = " GeeksforGeeks"

// providing subtitle for the ActionBar


[Link] = " [Link]"

[Link] 7/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

// adding icon in the ActionBar


[Link]([Link].gfg_logo_24x13)

// methods to display the icon in the ActionBar


[Link](true)
[Link](true)
}

// method to inflate the options menu when


// the user opens the menu for the first time
override fun onCreateOptionsMenu(menu: Menu): Boolean {
[Link]([Link], menu)
return [Link](menu)
}

// methods to control the operations that will


// happen when user clicks on the action buttons
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when ([Link]) {
[Link] -> [Link](this, "Search Clicked",
Toast.LENGTH_SHORT).show()
[Link] -> [Link](this, "Call Clicked",
Toast.LENGTH_SHORT).show()
[Link] -> [Link](this, "Copy Clicked",
Toast.LENGTH_SHORT).show()
}
return [Link](item)
}
}

Output:

[Link] 8/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

Step 4: Color of the ActionBar

Head over to [Link] file located in the values directory of the res
folder. To change the default color of the ActionBar, one has to change
the colorPrimary resource. Below is the code to make the ActionBar
color 'green'.

<resources xmlns:tools="[Link]
<!-- Base application theme. -->
<style name="[Link]" parent="[Link]">
<!-- Customize your light theme here. -->

<item name="colorPrimary">#0f9d58</item>
<item name="colorPrimaryDark">#006d2d</item>
<item name="colorAccent">#55cf86</item>
</style>

<style name="[Link]" parent="[Link]" />

</resources>

Output:

Run on Emulator:

[Link] 9/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

0:00

Advantages of ActionBar

Provides a customized area to design the identity of an app


Specify the location of the user in the app by displaying the title of the
current Activity.
Provides access to important and frequently used actions
Support tabs and a drop-down list for view switching and navigation.

Disadvantages of ActionBar

All features of the ActionaBar are not introduced at once but were
introduced with the release of different API levels such as API 15, 17,
and 19.
The ActionBar behaves differently when it runs on different API levels.
The features that were introduced with a particular API does not
provide backward compatibility.
[Link] 10/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

Note: To resolve all the problems related to ActionBar, Google


introduced ToolBar along with the release of API 21(Android
Lollipop). To know more, refer to ToolBar in Android with Example

ActionBar in Android with Example

Comment More info

Explore
Android Tutorial 10 min read

Basics

Software Setup and Configuration

Android Studio Tutorial 9 min read

File Structure & Components

[Link] 11/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

Core Topics

Layout & View

Button

Intent and Intent Filters

Toast & RecyclerView

Corporate & Communications Address:


A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305)

Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305

Company Explore
About Us POTD
Legal Job-A-Thon
Privacy Policy Community
Contact Us Blogs
Advertise with us Nation Skill Up
GFG Corporate Solution
Campus Training Program

Tutorials Courses
Programming Languages IBM Certification
DSA DSA and Placements
Web Technology Web Development
[Link] 12/13
9/29/25, 1:52 AM ActionBar in Android with Example - GeeksforGeeks

AI, ML & Data Science Programming Languages


DevOps DevOps & Cloud
CS Core Subjects GATE
Interview Preparation Trending Technologies
GATE
Software and Tools

Videos Preparation Corner


DSA Aptitude
Python Puzzles
Java GfG 160
C++ DSA 360
Web Development System Design
Data Science
CS Subjects

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

[Link] 13/13

You might also like