Hide a tab in the TabHost in Android

android, tabs, visibility

Solution

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.

Problem

``` 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); ```

Original source