Sunday, November 3, 2013

ActionBarCompat - Action Item's basics

This post discusses how to add Actionitem's using compat library. For configuring v7 libararies, look here.

My Menu acction_bar.xml file goes like this.

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

    <item
        android:id="@+id/action_settings"
        compat:showAsAction="always"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/action_help"   
        compat:showAsAction="never"
        android:title="@string/action_help"/>
    
</menu>

Observe the above XML contain's xmlns:compat attribute. This attribute is pointing to the Application resources. The attribute compat:showAsAction will bind at run time and thus uses compat library. It is very important to use xmlns:compat.

Use the Menu xml in my ActionBarActivity like

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.acction_bar, menu);
        return true;
    }


Output :

Action bar with Action bar compat libararies



Action bar compatible library configuration

Android is providing compat libraries for Android level grater than 7 i.e Andorid Os version 2.1 and above. In this post I would like to discuss the configuration of Actionbar Compat project.

  • Download V7 library project from here
  • Import the downloaded project into the current work space.
  • Build the project.
  • Create  New Android Project.
  • Add the imported project as the supporting project.
Properties of the new working project

As shown above, add the  supporting project as library for the current working project.
Now you are ready with the configuration. 

Post configuration:
Some points to be noted for using the App compat action bar are:
  • The Class which wish to use AppCompat features must extend with ActionBarActivity.
  • In the res - > values --> styles.xml, Modify the AppBaseTheme to
    • <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> for light theme.