Android code to hide title bar example / How to hide the title bar through code in android


Do you want to make current activity full screen? It is one of the simplest things you can do in Android code.
requestWindowFeature(int featureId) method of Activity can be used for this.
requestWindowFeature() method accepts constants defined in android.view.Window

One of the constants available in android.view.Window is FEATURE_NO_TITLE which is used to hide the title bar.

Add below statement in the onStart() of the activity class before rendering the layout.

this.requestWindowFeature(Window.FEATURE_NO_TITLE);



Source code:

package com.example.hidetitlebar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_main);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}


Screenshot: (With title bar)




Screenshot: (Without title bar)





To set full screen for the entire application, we can use the themes in manifest xml file. There are many full screen themes readily available in Android, such as Theme_Holo_NoActionBar_Fullscreen, Theme_Holo_Light_NoActionBar_Fullscreen, Theme_DeviceDefault_NoActionBar_Fullscreen, Theme_NoTitleBar_Fullscreen, Theme_Black_NoTitleBar_Fullscreen, Theme_Light_NoTitleBar_Fullscreen .....

I have used Theme.NoTitleBar.Fullscreen in the following sample,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hidetitlebar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.example.hidetitlebar.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Screenshot:








Reactions:

0 comments :

Post a Comment