Studyon Minte9.com
Android




Action Bar



* Adding support library

Adding the action bar when running on versions older than Android 3.0 (down to Android 2.1) 
requires that you include the Android Support Library in your application.

	Add support v7 to your app lib
		sdkextrasandroidsupportv7 
		androidMyFirstApplibsandroid-support-v7-appcompat
		(for Action Bar)

	Add icon
		Android_Design_Downloads_20130814IconsAll_Iconsholo_lighthdpiic_action_search.png
		androidMyFirstAppresdrawableic_action_search.png

Remember to properly set your app's API level support in the manifest: 
minSdkVersion="7"

	%--- AndroidManifest.xml

	<manifest ... >
	    <uses-sdk android:minSdkVersion="7"  android:targetSdkVersion="18" />
	    ...
	</manifest>


* Specify the actions in XML

Add an <item> element for each item you want to include in the action bar. For example:

	%--- res/menu/main_activity_actions.xml

	<menu xmlns:android="http://schemas.android.com/apk/res/android" >
	    <!-- Search, should appear as action button -->
	    <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>


* Add the Actions to the Action Bar

	%--- MainActivity.java

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

	    // Inflate the menu items for use in the action bar
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_activity_actions, menu);
	    return super.onCreateOptionsMenu(menu);
	}

Action Bar will apear on bootom for versions older than Android 3.0


* Respond to Action Buttons

When the user presses one of the action buttons or another item in the action overflow, 
the system calls your activity's onOptionsItemSelected() callback method.

	%--- MainActivity.java

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	   
	    // Handle presses on the action bar items
	    switch (item.getItemId()) {
		case R.id.action_search:
		    openSearch();
		    return true;
		case R.id.action_settings:
		    openSettings();
		    return true;
		default:
		    return super.onOptionsItemSelected(item);
	    }
	}

	public void openSearch() {}
	    
	public void openSettings() {}


http://developer.android.com/training/basics/actionbar/index.html
http://developer.android.com/tools/support-library/setup.html
http://developer.android.com/training/basics/actionbar/adding-buttons.html#XML