action_bar

action bar

1. intro

the action bar displays the title for the activity and the app icon on the left
android:minSdkVersion=”11”(ICS), use android:theme="@android:style/Theme.Holo" as theme parent will have ActionBar support.

2. add basic actionbar

create options menu

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout);
getActionBar().setDisplayHomeAsUpEnabled(true);//up button go to parent activity
}

handle options menu actions

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_call:
        return true;
    case R.id.action_search:
        Intent aI = new Intent(this, SearchActivity.class);
        startActivity(aI);
        return true;
    case R.id.action_settings:
        return true;
    case R.id.action_share:
        return true;
    }
    return super.onOptionsItemSelected(item);
}

3. actionbar in overlay mode

flower

create a custome theme

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- theme -->
    <style name="MEActionBarTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MEActionBar</item>
        <item name="android:windowActionBarOverlay">true</item>
    </style>
    <!-- ActionBar styles -->
    <style name="MEActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/transparent_background</item>
    </style>
    <color name="transparent_background">#3b000000</color>
</resources>

declare the theme

declare the activity theme as android:theme="@style/MEActionBarTheme"

toggle actionbar via click

hook onClick() event of the view object(TextView or RelativeLayout etc.) with following code:

@Override
public void onClick(View v) {
    if (getActionBar().isShowing()) {
        getActionBar().hide();
    } else {
        getActionBar().show();
    }
}

misc

themes can be applied to Activity/Application.

eg: Theme.Holo, Theme.Holo.Light,

google has a default Action Bar Icon Pack.