Wednesday, 5 February 2014

How use custom ActionBar layout - short guide

You need create custom layout first.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
         android:layout_width="match_parent"  
         android:layout_height="match_parent"  
         android:background="@color/aqua"  
         android:orientation="vertical">  
   <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_centerInParent="true"  
       android:textColor="#ffffff"  
       android:id="@+id/barTitle"  
       android:textSize="25dp"/>  

 </RelativeLayout>  

After this in our SherlockActivity need add some code in onCreate method.

 @Override  
 protected void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  

      getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);  
      getSupportActionBar().setCustomView(R.layout.customBar);  

      //element from our custom layout, textview is my component which acts like title  
      ((TextView) findViewById(R.id.barTitle)).setText("This is title"));  
      setContentView(R.layout.l1a2a_has_account);  
      
      //further stuff  

 }