Wednesday, December 25, 2013

Creating custom tab layout

1) create [drawablename].xml under /res/drawable


eg: tabbg.xml (ref: Creating Selection/Deselection drawable using images)


2) Add to layout.

eg: tablayout.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/tablayout"
   android:layout_width="wrap_content"
   android:background="@drawable/tabbg"    
   android:gravity="bottom"
   android:layout_height="wrap_content">


   <ImageView
       android:id="@+id/icon"
       android:layout_marginTop="1dp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/icn_bag"
       android:layout_centerHorizontal="true"
       android:src="@color/transparent" />


   <TextView
       android:id="@+id/title"        
       android:layout_below="@+id/icon"        
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"        
       android:textColor="@color/white"
       android:gravity = "center"
       android:ems="10" />

</RelativeLayout>


In extended TabActivity source:

//instead of getTabHost()

TabHost host= (TabHost) findViewById(android.R.id.tabhost); 
host.setup();  

//drawable for divider
host.getTabWidget().setDividerDrawable(R.drawable.menu_bg_gap);

//create spec TabHost.TabSpec spec = host.newTabSpec("Testtab");

// inflate the layout ViewGroup view = (ViewGroup)LayoutInflater.from(host.getContext()).inflate(R.layout.tablayout, null);

// add tab icon and title

//image drawable for tab icon
ImageView v = (ImageView)view.findViewById(R.id.icon);
v.setBackgroundDrawable(imageForTab);

// set tab title
TextView tv = (TextView) view.findViewById(R.id.title);  
tv.setText("tabtile");

spec.setIndicator(view);

// create tab activity intent
Intent intent = new Intent(this, 'Your tab Activity'.class);
spec.setContent(intent);

//add tab to host
host.addTab(spec);



No comments:

Post a Comment