Swayam
Swayam

Reputation: 16354

Hide a tab in the TabHost in Android

if( ......)
    {


        tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
                    //to hide the first tab in the TabHost

    }

Is there anything wrong with this code ? The application crashes when I add this code inside the onCreate() method.. Any idea ?

My LogCat :

05-31 22:03:38.471: E/AndroidRuntime(598): Caused by: java.lang.NullPointerException 05-31 22:03:38.471: E/AndroidRuntime(598): at swayam.dev.mushtouch.MushTouchActivity.setVisibilityControls(MushTouchActivity.j‌​ava:75) 05-31 22:03:38.471: E/AndroidRuntime(598): at swayam.dev.mushtouch.MushTouchActivity.onCreate(MushTouchActivity.java:220) 05-31 22:03:38.471: E/AndroidRuntime(598): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-31 22:03:38.471: E/AndroidRuntime(598): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

Also tried this code. Still keeps crashing.

getTabHost().getTabWidget().removeViewAt(0);

Upvotes: 3

Views: 9443

Answers (2)

wdziemia
wdziemia

Reputation: 1459

My response is going to be too long so ill put it in an answer.

So far you have

TabHost  tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);

You are getting a NullPointerException, meaning that whenever you are using that line of code, you are trying to change something that doesn't exist on screen, or possibly something off screen.

Check your Import statement for R.

Below your package statement you should have the following:

import your.package.R;

and not

import android.R;

Once that is fixed, when you reference your tab host, use the following:

TabHost  tabHost = (TabHost)findViewById(R.id.tabhost);

If that doesnt work, make sure the tabhost is actually on the screen and that your not in a seperate activity.

Upvotes: 15

fgeorgiew
fgeorgiew

Reputation: 1204

If you wanna hide Tab you should use:

getChildTabViewAt() instead of getChildAt()

so your code should looks like this:

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

Upvotes: 4

Related Questions