Tuesday, June 26, 2012

Hide Title bar in Android

A simple but useful code snippet to hide title bar in Android. The code snippet is

requestWindowFeature(Window.FEATURE_NO_TITLE);

The important thing is, you need to put this code in onCreate() of the activity class. That also before setContentView(). If we put the requestWindowFeature() after setContentView() then we will get an exception. The exception will look like,

E/AndroidRuntime(  277): FATAL EXCEPTION: main
E/AndroidRuntime(  277): java.lang.RuntimeException: Unable to start activity ComponentInfo 
android.util.AndroidRuntimeException: requestFeature() must be called before adding content


So the code will be like,
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
}

Androidians