Prevent status bar for appearing android (modified)
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);
Android 5.0, prevent status bar expansion
Finally the best I can do is to use the Task Lock feature introduced in Android 5.0.
Reference: startLockTask.
The issues of this method:
- Unless you have a helper app that grant lock task permission to your full screen app, the user will be asked if (s)he allows the lock.
- Once locked, the user can still use physical buttons (home, back, volumn, etc.).
Since my intention is not to have a kiosk mode, I don't really care about point 2.
Point 1 is also acceptable, but a bit annoying since it will (and should) be asked every time onResume
is called.
This can be solved by setting a service app as device owner, which grants permission to apps that demand it. But the user has to do this him(her)self (c.f. this answer), which is reasonable, otherwise an app would be able to totally kidnap your device.
Disable status and naviagation bars
It is not possible to disable the ability to swipe the bars back in. As a normal app, you are not allowed to take over the device and prevent the user from exiting the app.
If you're trying to develop a "kiosk mode"-type app, take a look at the APIs for dedicated devices.
Android Hide & Disable Notification (Status) Bar
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
Always disable status bar and bottom bar in a custom launcher
You have implemented the IMMERSIVE_STICKY
flag which allows the status bar to be called by top-down swipe on the top of the screen. To disable this:
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
setContentView(R.layout.your_layout);
To disable status drop down, use this method. This is an overlay over status bar and consumed all input events. It prevents 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.
Android Manifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
customViewGroup implementation
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);
Related Topics
Transfer Data Between iOS and Android via Bluetooth
Non-Scrollable Listview Inside Scrollview
Installation App Blocked by Play Protect
Android Dex Gives a Bufferoverflowexception When Building
How to Set Layout_Gravity Programmatically
How to Start Service-Only Android App
How to Set Edittext to Only Accept Numeric Values in Android
Change Activity's Theme Programmatically
Multi-Line Edittext with Done Action Button
How to Convert Dp, Px, Sp Among Each Other, Especially Dp and Sp
How to Save Custom Arraylist on Android Screen Rotate
Prevent Status Bar for Appearing Android (Modified)
Dialogs/Alertdialogs: How to "Block Execution" While Dialog Is Up (.Net-Style)
How to Disable the Clear Data Button in Application Info of Manage Application
Programmatically Check Play Store for App Updates
Misbehavior When Trying to Store a String Set Using Sharedpreferences