articles

Action Bar with menu in Android

Manoj Pandey4877 20-Feb-2015

Previously, we learn about Android Persistence with Shared Preferences. Now we see how to use Android Persistence with Shared Preferences.

The action bar (Action Bar) is located at the top of the activity. It can display the activity title, icon, actions which can be triggered, additional views and other interactive items. It can also be used for navigation in your application.

The action bar is enabled for devices which specify a target SDK of API version 11 or higher. It is possible to disable the action bar via the used theme, but the default Android themes have it enabled.

1.      Create an Android project and minimum api must be greater than 13

2.      Add menu item in main menu 

<menu 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"
    tools:context=".MainActivity" >
    <item
          android:id="@+id/action_search"
          android:icon="@android:drawable/ic_menu_search"
          android:showAsAction="ifRoom"
          android:title="@string/action_search"/>
    <item
          android:id="@+id/action_record"
          android:icon="@android:drawable/btn_star_big_on"
          android:showAsAction="ifRoom"
          android:title="@string/action_fav"/>
    <item
          android:id="@+id/action_save"
          android:icon="@android:drawable/ic_btn_speak_now"
          android:showAsAction="ifRoom"
          android:title="@string/action_rec"/>
    <item
          android:id="@+id/action_label"
          android:icon="@android:drawable/ic_menu_camera"
          android:showAsAction="ifRoom"
          android:title="@string/action_Camera"/>
    <item
          android:id="@+id/action_play"
          android:icon="@android:drawable/ic_menu_call"
          android:showAsAction="ifRoom"
          android:title="@string/action_Call"/>
    <item
          android:id="@+id/action_play2"
          android:icon="@drawable/ic_launcher"
          android:showAsAction="ifRoom"
          android:title="@string/action_emergrncy"/>
</menu>

3. Add String name in res/values/string.xml 

<?xml version="1.0"   encoding="utf-8"?>
<resources>
    <string name="app_name">ActionBarTest</string>
    <string name="action_settings">Settings</string>
    <string name="action_search">Search</string>
    <string name="action_fav">Favorites</string>
    <string name="action_rec">Recording</string>
    <string name="action_Camera">Camera</string>
    <string name="action_Call">Phone Call</string>
    <string name="action_emergrncy">Emergency</string>
    <string name="hello_world">Hello world!</string>
</resources>

4. Add following code in MainActivity.class

   package com.example.actionbartest;
   import android.os.Bundle;
   import android.app.Activity;
   import android.view.Menu;       public class MainActivity extends Activity { 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);   
     } 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
           // Inflate the menu; this adds items to the action bar if it is present.
           getMenuInflater().inflate(R.menu.main, menu);
           return true;
     }} 

 

Now run your application

  Action Bar with menu in Android  Action Bar with menu in Android

 

If you want to split your action bar then add following code in AndroidManifest.xml file 

<application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme"

        android:uiOptions="splitActionBarWhenNarrow" >

 

        <!-- For support less than 14 api -->

        <meta-data

            android:name="android.support.UI_OPTIONS"

            android:value="splitActionBarWhenNarrow" />

 And run your application

 Action Bar with menu in Android  Action Bar with menu in Android


Updated 07-Sep-2019

Leave Comment

Comments

Liked By