Android - Tips'n Tricks - Hiding/Showing the Notification bar

Maybe you don't know that but it's possible to remove the Notification bar programmatically.

It's quite useful if you don't really need it for your custom application.

It takes a little bit portion of your screen but why let it?

Let's see that in this Notification bar tutorial.

First of all

We're going to create a Button to hide and display the NotificationBar.

Each time you click the Button, the state changes by toggling the features of this Button.

Code

MainActivity.java

// BadproG.com
package com.badprog.android.tutorial.removingbar;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BadprogButton bb = new BadprogButton(this);
  }
}

 

BadprogButton.java



// BadproG.com
package com.badprog.android.tutorial.removingbar;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

/**
* BadprogButton class
*/
public class BadprogButton {

  public Button     _button_1;
  public Activity   _activity;

  boolean _state_bar_notification     = false;

  /**
   * Sets the main global variables and launches the
   * buttonDisplayHideBarNotification() method.
   * @param a
   */
  BadprogButton(AppCompatActivity a) {
    _activity = a;
    _button_1 = (Button) _activity.findViewById(R.id.xml_button_1);
    buttonDisplayHideBarNotification();
  }

  /**
   * By pressing this button, we hide or display the NotificationBar.
   */
  void buttonDisplayHideBarNotification() {
    _button_1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        if (false == _state_bar_notification) {
          _activity.getWindow().addFlags(             // Hiding notification bar
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);
          _button_1.setText("Show NotificationBar");
          _state_bar_notification = true;
        } else {
          _activity.getWindow().clearFlags(           // Showing notification bar
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);
          _button_1.setText("Hide NotificationBar");
          _state_bar_notification = false;
        }
      }
    });
  }

}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.badprog.android.tutorial.removingbar.MainActivity"
    android:id="@+id/activity_main">

    <Button
        android:id="@+id/xml_button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hide NotificationBar"
        android:textAllCaps="false"
        />

</RelativeLayout>

Conclusion

Well done you've made it! cool

 

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.