Android hide status bar

In this Android tip, I am going to show you how to hide status bar on different versions of Android system. Hiding the status bar allows your UI components to use more space of the screen. When the status bar is hidden, the action bar should be hidden.
There are two approachs to hide the status bar. In the first approach, you hide the status bar by setting the activity theme in the AndroidManifest.xml file as shown below. Hiding the status bar using this approach is best if the status bar keeps hidden in your applications.

<application
   ...
   android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
   ...
</application>

If you allow the user to show and hide the status bar in your applications, you will use the alternative approach. In this approach, you hide the status bar programmatically. On Android 4.0 and Lower, you can hide the status bar by using the Window class. You call the setFlags() method to hide or show the status bar. On Android 4.1 and Higher, you also can use the Window class to hide the status bar. However, instead of using the setFlags() method, you use the getDecorView() method to get the top-level window décor view and call the setSystemUiVisibility() method of the décor view object to hide or show the status bar.

Now to have a workable example application on hiding the status bar, create a new Android project. Then modify the activity_main.xml file to add two Buttons as shown below. The first button is clicked to hide the status bar and the second button is clicked to redisplay the status bar.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"
  android:orientation="horizontal"

  >
  <Button
android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Hide status bar"
   android:onClick="hideStatusBar"
     />
<Button
    
android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Show status bar"
   android:onClick="showStatusBar"
     />

</LinearLayout>


In the MainActivity class, you need to add two methods: hideStatusBar() and showStatusBar() to hide and show the status bar when the user clicks the Hide status bar and Show status bar buttons.

package com.example.androidexample;
import android.os.Build;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void hideStatusBar(View view){

//Hide the status bar on Android 4.0 and Lower
if (Build.VERSION.SDK_INT < 16) {
Window w=getWindow();
     w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
           WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
   }
else{
View decorView = getWindow().getDecorView();
// Hide the status bar.
int visibility = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(visibility);
// Hide action bar that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();

}

}
public void showStatusBar(View view){
// Show the status bar on Android 4.0 and Lower
if (Build.VERSION.SDK_INT < 16) {
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
           WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
   }
//Show the status bar on Android 4.1 and higher
else{
View decorView = getWindow().getDecorView();
// Show the status bar.
int visibility = View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(visibility);
// Show action bar
ActionBar actionBar = getActionBar();
actionBar.show();

}

}

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

}

Android-hide-status-bar

Posted by: Dara | post date: 09-25-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.