1

I have a TabHost holding 5 tabs. As far as I can see, there has to be one tab selected at all times.

I need a way to unselect all my tabs so none will be selected.

If the tabhost is meant by general to have one tab selected at all times, how can I make it appear (UI speaking) as if the tab isn't selected?

Bart
  • 19,692
  • 7
  • 68
  • 77
Binary
  • 147
  • 2
  • 14

3 Answers3

4

try this:

final TabWidget tabWidget = tabHost.getTabWidget();
final int n = tabWidget.getChildCount();
for (int i = 0; i < n; ++i) {
    tabWidget.getChildAt(i).setSelected(false);
}

or you can add hidden tab and select it when you want unselect a tab

tabHost.addTab(
            tabHost.newTabSpec("hiddenTab").setIndicator(""),
            MyFragment.class,
            null
    );

tabHost.getTabWidget().getChildTabViewAt(hiddenTabIndex).setVisibility(View.GONE);

and select this tab when you want

tabHost.setCurrentTab(hiddenTabIndex);
Anton Klimov
  • 359
  • 4
  • 10
1

This is not possible AFAIK. but yes,you can set the selected tab's color to look like it is unselected and set a blank layout over it by managing a global variable when you make it 'unselected' and setting up normal layout when you want it to be shown normally to user. But this is kind of a trick.

Hope,you get my point!

EDIT :

Suppose you have set String what="disappear" somewhere in your code to show it 'unselected',then you can use this function to change color of tab:

Main.class:

//Change The Backgournd Color of Tabs
    public void setTabColor(TabHost tabhost) {


        for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
        {
                tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"))); //unselected white colored                   
        }

            if(!what.equals("disappear"))
                 tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("FF0000"))); // selected red colored

            else
                 tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("FFFFFF"))); // selected but show as unselected with white color


    }

And in your activity class(which is opened by that selected tab):

FirstActivity.class:

if(what.equals("disappear"))
      setContentView(R.layout.blank);
else
      setContentView(R.layout.first_layout);

blank.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="fill_parent"
  android:orientation="vertical"
  android:id="@+id/layout"
  android:background="#ffffff"  
  android:gravity="center">
  <!-- You can make background transperent by setting it to "00ffffff" -->
  <!-- You can also add this textview to guide user -->
  <!--
      <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Click Any Tab To Start
         />
  -->
</LinearLayout>
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • Can you provide me with some basic follow up steps on how to do this? Like if there are System colors I have to use for this? How exactly to create the blank layout and set it as the specific tab's? – Binary Nov 03 '11 at 11:17
  • first, thanks for helping. Now, I've managed to change the tab's color using your code, and I wan't to change the tab's background using setBackgroundDrawable(). I'm trying to get the original drawable for an unselected tab. I found the resource's name is: tab_unselected.9 but I don't know how to use it as the tab's backgroundDrawable. I tried tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_unselected.9)); But obviously it's wrong. How can I use that resource? or is there a smarter way to get the original drawable? – Binary Nov 03 '11 at 12:34
  • @Eitan: Sorry,i have no idea regarding this. :( – Hiral Vadodaria Nov 03 '11 at 12:40
0

For this purpose, maybe using tabHost is not the proper way ?

Mathieu de Brito
  • 2,536
  • 2
  • 26
  • 30
  • What do you propose as an alternative? – Binary Nov 03 '11 at 11:26
  • Well, I was thinking of a global layout containing all your tabs in the hidden mode (`View.GONE`) expected the one you want to show. But you will have to create you own layout containing the tabs headers, so that when you want to unselect all tabs, you will ba able to set the background you want on the tabs headers. That is not really clean, but it works. Hope you will not have to do this ;) – Mathieu de Brito Nov 03 '11 at 12:59