16

I am very new to android. Now I build a small application.

I need to change the look and feel of android default look up in 2.2. So, here I try to change the background of the tabs. Can you help me to do that.

I love the way using the xml/style.

This is what I need that actual output.

Tabs

Alexey
  • 1,198
  • 1
  • 15
  • 35
Sibiraj PR
  • 1,481
  • 1
  • 10
  • 25

1 Answers1

46

Your Tab Host XML file

TabHost

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

    <LinearLayout android:orientation="vertical" 
        android:layout_width="fill_parent" android:layout_height="fill_parent"> 

        <TabWidget android:id="@android:id/tabs" 
            android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
        <FrameLayout android:id="@android:id/tabcontent" 
            android:layout_width="fill_parent" android:layout_height="fill_parent"> 
        </FrameLayout> 

    </LinearLayout> 

</TabHost> 

in Your Main Activity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);

    setupTab(new TextView(this), "Tab 1");
    setupTab(new TextView(this), "Tab 2");
    setupTab(new TextView(this), "Tab 3");
}

private void setupTab(final View view, final String tag) {
    View tabview = createTabView(mTabHost.getContext(), tag);
    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
        public View createTabContent(String tag) {
            return view;
        }
    });
    mTabHost.addTab(setContent);
}

private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}

CustomTabsLayout tabs_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabsLayout" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
    android:padding="10dip" android:gravity="center" android:orientation="vertical">

    <TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Title"
        android:textSize="15dip" android:textColor="@drawable/tab_text_selector" />
</LinearLayout>

tab_text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@android:color/white" />
    <item android:state_focused="true" android:color="@android:color/white" />
    <item android:state_pressed="true" android:color="@android:color/white" />
    <item android:color="#f8f8f8" />
</selector>

tab_bg_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!--  Active tab -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
    <!--  Inactive tab -->
    <item android:state_selected="false" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
    <!--  Pressed tab -->
    <item android:state_pressed="true" android:drawable="@android:color/transparent" />
    <!--  Selected tab (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
    android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>

tab_bg_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
        android:endColor="#696969" android:angle="-90" />
</shape>

tab_bg_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#5C5C5C" android:centerColor="#424242"
    android:endColor="#222222" android:angle="-90" />
</shape>

And Finally in your main activity class

mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

And finish :)

  • Thanks for that long answer. I will get back to you after i implement it. – Sibiraj PR Jul 27 '13 at 14:46
  • Is this help to you? :) – Abdulkadir NURKALEM Oct 04 '13 at 14:08
  • @AbdulkadirNURKALEM can you please guide me on what is tab_divider in mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); – Hassaan Rabbani Dec 13 '13 at 10:32
  • @SibirajPR can you please guide me on what is tab_divider in mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); – Hassaan Rabbani Dec 13 '13 at 10:33
  • tabhost giving me nullpointerexception .. Does this work for anyone? – Dusean Singh Jan 30 '14 at 19:33
  • @AbdulkadirNURKALEM Waht is tab_devider here ? – Shabbir Dhangot Apr 12 '14 at 12:17
  • 2
    getting error at `mTabHost.addTab(setContent);` do any one have any idea getting `null pointer exception` – Kartheek Sarabu May 29 '14 at 05:22
  • 1
    Answer to Kartheek's Question = Add mTabHost.setup(); to the onCreate method. Like so: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTabHost = (TabHost) findViewById(android.R.id.tabhost); mTabHost.setup(); setupTab(new TextView(this), "Tab 1"); setupTab(new TextView(this), "Tab 2"); setupTab(new TextView(this), "Tab 3"); } – Gene Aug 13 '14 at 21:52
  • Thanx a lot....But there is one line of code missing which is causing NullPointerException i.e. After this line in MainActivity mTabHost = (TabHost) findViewById(android.R.id.tabhost); ......write mTabHost.setup();....All the best – pioneerBhawna Sep 22 '14 at 06:39
  • where is R.drawable.tab_divider xml? – Shihab Uddin Dec 19 '15 at 06:10