Android Action Bar Tutorial

                   In this android action bar tutorial,we are going to learn how to implement action items and menu items in the action bar in android.In the latest version 4.2 Android Jellybean supporting applications mostly using action bar with action items and menu items.

So lets start learn how to implement it in the action bar.

Before learn more about action bar menu items learn Android Menu Tutorial

STEP BY STEP

1.Create an Android Application

Refer:-How to create Android Application

2.Here we are adding menu items in the project.So we need to add menu items.

So select res—>menu folder otherwise create a folder named “menu” in the res folder.

Then create a android xml file named main.xml

Then we need to add items into it.

The code is shown bellow.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item

android:id="@+id/action_settings"

android:orderInCategory="100"

android:showAsAction="never"

android:title="@string/action_settings"/>

<item

android:id="@+id/apps"

android:showAsAction="ifRoom|withText"

android:icon="@drawable/apps"

android:title="@string/apps"></item>

<item

android:id="@+id/games"

android:title="@string/games"

android:icon="@drawable/games"

android:showAsAction="ifRoom|withText"

/>

</menu>

 

3.Now we add menu items,but we need to add string variables also.

So select “values” folder from res folder and open strings.xml

Then add as follows

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

<resources>

<string name="app_name">AndroidActionbar</string>

<string name="action_settings">Settings</string>

<string name="hello_world">Hello world!</string>

<string name="apps">Apps</string>

<string name="games">Games</string>

</resources>

4.Here we are using two images for the menu items.

   So we need to add it in the respective drawable folder.

   You can include your own images to each of the drawable folder.

5.Now we are going to add the menu items into our MainActivity.java class

The code is shown bellow

package com.example.androidactionbar;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;

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)

{

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item)

{

super.onOptionsItemSelected(item);

switch(item.getItemId())

{

case R.id.apps:

Toast.makeText(getBaseContext(), "Android Apps", Toast.LENGTH_SHORT).show();

break;

case R.id.games:

Toast.makeText(getBaseContext(), "Android Games", Toast.LENGTH_SHORT).show();

break;

}

return true;

}

}

Here we are implementing onCreateOptionsMenu and onOptionsItemSelected methods for displaying menu items

 

6.Lets run the android application

 

7.Output is shown bellow

actionbar1

 

:Lets see the output horizontaly.

To view result in emulator horizontaly Press Ctrl +F11

Lets see

actionbar2

To learn more Android Emulator Shortcuts

Refer:-Android Emulator Shortcuts