Search...

Sunday, November 13, 2011

How to add custom layout in honeycomb ActionBar in android

Step1: Create your customview
e.g: sample_custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent" android:layout_height="wrap_content">


      <Button android:visibility="visible" android:text=""
            android:id="@+id/button" android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="10dp" android:paddingLeft="10dp"></Button>

</LinearLayout>

Step2 : Put the below code on your activity OnCreate() method
protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);


ActionBar bar= getActionBar();
View actionCustomView = getLayoutInflater().inflate(
                        R.layout. sample_custom_layout,null);
            bar.setCustomView(actionCustomView, new ActionBar.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                        Gravity.RIGHT));
           
            int change = bar.getDisplayOptions() ^ ActionBar.DISPLAY_SHOW_CUSTOM;
            bar.setDisplayOptions(change, ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);

}

2 comments:

  1. For all the crazy colors in this post this is actually exactly what I was looking for. StackOverflow didn't even have this solution!

    ReplyDelete
  2. Thank, works fine.

    ReplyDelete