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



No comments:

Post a Comment