I've been able to hide the notification bar by going full screen, by using the code below
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"
or
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
But what I am trying to do is completely disable the status bar. I'm in what's known as a "kiosk mode" and I'd like to make sure that a user can not slide their finger down from the top bezel. Both of the solutions above work for hiding the notification bar, but it does not work for disabling it completely within the app.
Is this possible?
Instead of following links to other answers, here's what I did.
This solution does not disallow a user to "view" the status bar in it's 'preview' state if pulled down (even in a full screen app), but it DOES disallow a user from pulling the status bar down to it's full state to see settings, notifications, etc.
You must first add the permissions in your AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Then add another class (Java file) called customViewGroup.java
and place this code in it:
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
public class customViewGroup extends ViewGroup {
public customViewGroup(Context context) {
super(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
}
After you have both of those set up, you can then add this into your main onCreate()
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);
This solution disables the ability to pull the status bar down always, until your app is closed. You'll have to remove this action on pause if you don't want to close your app every time.
Credit goes to @Abhimaan Madhav from This Answer
I think permanently disable the status bar is difficult. I am also working on the same concept and did lots of R&D and found that below code can be useful. if the user tries to expand the status bar then within a sec it will pull back it and it will work on oreo as well. I have tried on different OS.
public class BlockStatusBar { Context context;
// To keep track of activity's window focus
boolean currentFocus;
// To keep track of activity's foreground/background status
boolean isPaused;
public Handler collapseNotificationHandler;
Method collapseStatusBar = null;
public BlockStatusBar(Context context, boolean isPaused) {
this.context = context;
this.isPaused = isPaused;
collapseNow();
}
public void collapseNow() {
// Initialize 'collapseNotificationHandler'
if (collapseNotificationHandler == null) {
collapseNotificationHandler = new Handler();
}
// If window focus has been lost && activity is not in a paused state
// Its a valid check because showing of notification panel
// steals the focus from current activity's window, but does not
// 'pause' the activity
if (!currentFocus && !isPaused) {
Runnable myRunnable = new Runnable() {
public void run() {
// do something
try {
// Use reflection to trigger a method from 'StatusBarManager'
Object statusBarService = context.getSystemService("statusbar");
Class<?> statusBarManager = null;
try {
statusBarManager = Class.forName("android.app.StatusBarManager");
} catch (ClassNotFoundException e) {
Log.e(LOG_TAG, "" + e.getMessage());
}
try {
// Prior to API 17, the method to call is 'collapse()'
// API 17 onwards, the method to call is `collapsePanels()`
if (Build.VERSION.SDK_INT > 16) {
collapseStatusBar = statusBarManager.getMethod("collapsePanels");
} else {
collapseStatusBar = statusBarManager.getMethod("collapse");
}
} catch (NoSuchMethodException e) {
Log.e(LOG_TAG, "" + e.getMessage());
}
collapseStatusBar.setAccessible(true);
try {
collapseStatusBar.invoke(statusBarService);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// Check if the window focus has been returned
// If it hasn'kioskthread been returned, post this Runnable again
// Currently, the delay is 100 ms. You can change this
// value to suit your needs.
if (!currentFocus && !isPaused) {
collapseNotificationHandler.postDelayed(this, 100L);
}
if (!currentFocus && isPaused) {
collapseNotificationHandler.removeCallbacksAndMessages(null);
}
} catch (Exception e) {
Log.e("MSG", "" + e.getMessage());
}
}
};
// Post a Runnable with some delay - currently set to 300 ms
collapseNotificationHandler.postDelayed(myRunnable, 1L);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With