ark
ark

Reputation: 167

Changing background of tabhost workds in ICS but is causing problems in lower versions

To make the tabhost transparent i have implemented the following code..

    for(int i = 0; i < th.getTabWidget().getTabCount(); i++)
    {
    th.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
    }

This changes the background of all the tabs to transparent successfully in ICS but when I test it on 2.2 and 2.3 the first 3 tabs backgrounds are transparent and the last 3 are not(I have 6 tabs in all)(incidentally the 1st 3 tabs are the ones that appear when that activity gets started)

What is causing this and how can i resolve it... Please help..

After applying your suggestion this is what i included in the code,

        th.setOnTabChangedListener(new OnTabChangeListener(){
        @Override
        public void onTabChanged(String tabId) {
            manageTabBackgrounds();
        }

        private void manageTabBackgrounds() {
            // TODO Auto-generated method stub
            for(int i = 0; i < th.getTabWidget().getTabCount(); i++)
            {
            th.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
            }
        }           
    });

However this seems to have had no effect on the tabs... Please guide..

Upvotes: 0

Views: 910

Answers (3)

ark
ark

Reputation: 167

This is what finally worked for me..

  th.setOnTabChangedListener(new OnTabChangeListener(){
        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub
             for(int i=0;i<th.getTabWidget().getChildCount();i++)
                {
                   th.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT); //unselected
                }
                th.getTabWidget().getChildAt(th.getCurrentTab()).setBackgroundColor(Color.TRANSPARENT); // selected
        }
        });

Upvotes: 1

Tai Tran
Tai Tran

Reputation: 1404

Did you try this?

<TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/transparent">
    </TabWidget>

Upvotes: 0

momo
momo

Reputation: 3935

Make sure you do it onTabChanged...

Stick what you have in a function (e.g., "manageTabBackgrounds", then:

yourTabHostInstance.setOnTabChangedListener(new OnTabChangeListener(){
    @Override
    public void onTabChanged(String tabId) {
        manageTabBackgrounds();
    }           
});

Upvotes: 1

Related Questions