Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent status bar for appearing android (modified)

I am implementing a kiosk mode application and i have successfully made the application full-screen without status bar appearance post 4.3 but unable to hide status bar in 4.3 and 4.4 as status-bar appears when we swipe down at the top of the screen.

I have tried to make it full screen by

  • speciflying the full screen theme in manifest
  • setting window Flags ie setFlags
  • setSystemUiVisibility

Possible duplicate but no concrete solution found

Permanently hide Android Status Bar

Finally the thing i want is, how to hide status bar permanently in an activity?? in android 4.3,4.4,5,6versions

like image 890
Abhimaan Avatar asked Aug 13 '14 10:08

Abhimaan


People also ask

How do I hide the bars on my Android?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

How can I hide my status bar icon in Android?

How do I hide the status bar on Android? With a stock Android phone, press and hold Settings until System Settings appears. Select System UI Tuner > Status Bar, and turn off all the options.


2 Answers

In Android M you have to get an extra permission for making overlays. android.permission.SYSTEM_ALERT_WINDOW is not enough! So I used the code from the answer of Abhimaan within disableStatusBar() and had to make an intent to open the right settings dialog. I also added removing view in onDestroy() in order to enable status bar when the app exits. I also reduced the overlay height to 40 as it seems to be enough. Code works with 5.1 and 6.0 here.

public static final int OVERLAY_PERMISSION_REQ_CODE = 4545; protected CustomViewGroup blockingView = null;  @Override protected void onDestroy() {      super.onDestroy();      if (blockingView!=null) {         WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));         manager.removeView(blockingView);     } }   @Override protected void onCreate(Bundle savedInstanceState) {          if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {              if (!Settings.canDrawOverlays(this)) {                 Toast.makeText(this, "Please give my app this permission!", Toast.LENGTH_SHORT).show();                 Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + getPackageName()));                 startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);             } else {                 disableStatusBar();             }         }         else {             disableStatusBar();         } }   @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {      super.onActivityResult(requestCode, resultCode, data);      if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {         if (!Settings.canDrawOverlays(this)) {             Toast.makeText(this, "User can access system settings without this permission!", Toast.LENGTH_SHORT).show();         }         else         { disableStatusBar();         }     } }  protected void disableStatusBar() {      WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));      WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();     localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;     localLayoutParams.gravity = Gravity.TOP;     localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |              // this is to enable the notification to receive touch events             WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |              // Draws over status bar             WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;      localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;     localLayoutParams.height = (int) (40 * getResources().getDisplayMetrics().scaledDensity);     localLayoutParams.format = PixelFormat.TRANSPARENT;      blockingView = new CustomViewGroup(this);     manager.addView(blockingView, localLayoutParams); } 
like image 36
Alexey Ozerov Avatar answered Sep 22 '22 09:09

Alexey Ozerov


We could not prevent the status appearing in full screen mode in kitkat devices, so made a hack which still suits the requirement ie block the status bar from expanding.

For that to work, the app was not made full screen. We put a overlay over status bar and consumed all input events. It prevented the status from expanding.

note:

  • customViewGroup is custom class which extends any layout(frame,relative layout etc) and consumes touch event.
  • to consume touch event override the onInterceptTouchEvent method of the view group and return true

Updated

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>  

customViewGroup implementation

Code :

WindowManager manager = ((WindowManager) getApplicationContext()             .getSystemService(Context.WINDOW_SERVICE));  WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(); localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; localLayoutParams.gravity = Gravity.TOP; localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|              // this is to enable the notification to recieve touch events             WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |              // Draws over status bar             WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;      localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;     localLayoutParams.height = (int) (50 * getResources()             .getDisplayMetrics().scaledDensity);     localLayoutParams.format = PixelFormat.TRANSPARENT;      customViewGroup view = new customViewGroup(this);      manager.addView(view, localLayoutParams); 
like image 54
Abhimaan Avatar answered Sep 20 '22 09:09

Abhimaan