Introduction
Key Functions of an Action Bar
-
A dedicated space for giving your application identity and indicating the user's location in the application
-
Access to important actions in a predictable way (for example "Search")
-
Support for navigation and view switching ("with tabs" or "dropdown lists")
Setting up the Action Bar
Support Android 3.0 and Above Only
Adding Action Bar

Support Version Below 3.0
-
Let us suppose we use Android version 2.1 then adding the Action Bar requires that you include the Android Support library in the application we are developing.
-
Integrate the support library with our project.
- Update the activity so that it will extend ActionBarActivity. For example:
- public class MainActivity extends ActionBarActivity {.....}
We must update the <application> element or individual <activity> elements to use one of the Theme.AppCompat themes. For example let us look at the following line of code:
- <activity android : theme="@styles/theme.AppCompat.Light"...>
Adding Action to Action Bar
- <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=".ActionActivity">
- <item android:id="@+id/action_search"
- android:icon="@drawable/ic_action_search"
- android:title="@string/action_search"
- android:showAsAction="ifRoom" />
- // Settings, should always be in the overflow
- <item android:id="@+id/action_settings"
- android:title="@string/action_settings"
- android:showAsAction="never" />
- </menu>
If one is using a Support library
- <item android:id="@+id/action_search"
- android:icon="@drawable/ic_action_search"
- android:title="@string/action_search"
- myapp:showAsAction="ifRoom" />
- ...
- package com.example.gkumar.actionbars;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- public class ActionActivity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_action);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_action, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- switch (item.getItemId()) {
- case R.id.action_search:
- openSearch();
- return true;
- case R.id.action_settings:
- openSettings();
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
- }

Praveen KumarPosted Jan 16, 2015, 11:26 PM
Very Nice Gaurav Kumar