Draw over navigation bar (and other apps) on Android version >= 5 - android

I'd like to draw a (mouse pointer) icon on screen over the other applications from a service. I have implemented the functionality and I can draw over the screen, apart from the navigation bar. I've researched several other questions here and tried TYPE_SYSTEM_OVERLAY, TYPE_TOAST, TYPE_SYSTEM_ERROR and some other window types without success.
I'm not trying to capture focus, just drawing a mouse pointer on the screen for a second or two. When I try to draw over navigation bar, it just goes under (actually, the RelativeLayout ends on the border with navigation bar - even when I specify manual dimension for height). The screenshot below shows the hand pointer in the lower right part of the screen. That's as low as I can position it. Note that I'm not trying to hide the navigation bar in my application - trying to draw over other apps.
I even tried setting the xpos and ypos offset settings in WindowManager.LayoutParams instance, but that just offsets the layout and still goes below the navigation bar).
The layout params I'm using to show this window:
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSPARENT);
I am then adding the RelativeLayout with those parameters to the WindowManager: windowManager.addView(relativeLayout, params);

After a lot of fiddling, I've managed to get the right flags to make it work. It almost works (for most apps). The code to achieve follows (an example on button click handler):
#Override
public void onClick(View view) {
if (_windowManager == null) {
_windowManager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
}
WindowManager.LayoutParams params;
// create the view on the first try
if (_overlayView == null) {
ImageView hand;
_overlayView = new FrameLayout(MainActivity.this);
hand = new ImageView(MainActivity.this);
hand.setImageResource(R.drawable.icon_hand_pointer);
int w = hand.getDrawable().getIntrinsicWidth();
int h = hand.getDrawable().getIntrinsicHeight();
_overlayView.addView(hand);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
params.width = w;
params.height = h;
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = _xOffset;
params.y = _yOffset;
_windowManager.addView(_overlayView, params);
} else {
// move the view
params = (WindowManager.LayoutParams) _overlayView.getLayoutParams();
params.x = _xOffset;
params.y = _yOffset;
_windowManager.removeView(_overlayView);
_overlayView.setLayoutParams(params);
_windowManager.addView(_overlayView, params);
}
_xOffset += 40;
_yOffset += 100;
}
Here is a screenshot of this code working in an app (you can see the hand overlay; this is on Nexus 5 with Android 6.0.1:

The navigation bar at the bottom is something that cannot be drawn over because the items drawn above might impede access to the home button, back button, etc. But you can make your activity full-screen, meaning that it will hide the navigation buttons until you swipe up from the bottom, so that you can be free to draw anywhere on the screen
EDIT: I found some similar questions and this one has an answer : Draw bitmaps on top of the Navigation bar in Android
The answer was to add a y offset that's the size of the navigation bar.

Related

Blocking status bar in top down swiping

I've a full screen application covering the entire screen, top status bar included.
Since a top / down swipe is enabled to show some options to the user, it happens that swiping from top to down, the status bar is showing (as when you want to see notifications and swipe top down).
Is there a way to avoid this ?
Use type TYPE_SYSTEM_ERROR for WindowManager.LayoutParams, to create hide impossible fullscreen view.
Swipes for show status bar and navigation will be blocked.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMainLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_fullscreen, null);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
WindowManager.LayoutParams handleParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
handleParams.gravity = Gravity.TOP;
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(mMainLayout, handleParams);
}
And you should be show it after display power on (unlock screen) if needs. Add code:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
There are a couple of options. Before the line
super.onCreate(savedInstanceState)
add the following:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
This should completely get rid of the status bar. Basicaly the feature no title removes the top toolbar, and to ensure fullscreen size we are setting fullscreen flags for height and width.
As far as preventing from swiping at all, there is a trick that can be used. Basically place an invisible view at the top once you are in true fullscreen that prevents any swiping. For example,
View disableStatusBarView = new View(context);
WindowManager.LayoutParams handleParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
<height of the status bar>,
// This allows the view to be displayed over the status bar
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
// this is to keep button presses going to the background window
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,
PixelFormat.TRANSLUCENT);
handleParams.gravity = Gravity.TOP;
context.getWindow().addView(disableStatusBarView, handleParams);
This creates an invisible view over the status bar that will receive the touch events and block the events from reaching the status bar, therefore preventing it to be expanded.
Alternatively, and this works the best, is to override the windowFocusChanged method. Essentially you are NOT preventing the status bar from expanding, but YOU ARE preventing use. Because once expanded, it is closed. I say that this works best because with the invisible view methodology, you may want to consider the dimensions of the screen to really make it effective for all device. So there is more work.
First, declare permission in manifest
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
Then Override onWindowFocusChanged method,
public void onWindowFocusChanged(boolean hasFocus)
{
try
{
if(!hasFocus)
{
Object service = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method collapse = statusbarManager.getMethod("collapse");
collapse .setAccessible(true);
collapse .invoke(service);
}
}
catch(Exception ex)
{
}
}

Overlay Status bar on android 4.2.2+

I want to overlay android status bar. In my case it is on top. I do not want to overlay or hide navigation bar.
Note: Solution must work on android 4.2.2+. I prefer answers for non-rooted device.
I have searched many SO questions and answers, but none works on 4.2.2.
Below is my code, but it does not consume touch events. That's why status bar opens its panel. And I do not want this.
#Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
int statusBarHeight = (int) Math.ceil(25 * getResources().getDisplayMetrics().density);
overlay = new Button(this);
overlay.setBackgroundColor(Color.GREEN);
overlay.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("StatusBar", "touched");
return false;
}
});
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
statusBarHeight,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH|
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.RIGHT;
windowManager.addView(overlay, params);
}
#Override
public void onDestroy() {
super.onDestroy();
if (overlay != null) windowManager.removeView(overlay);
}
In main activity I start service:
startService(new Intent(this, StatusBarService.class));
And permission is added in AndroidManifest.xml:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
And now proof it can be done and it works on 4.2.2:
Play Google - MobiLock
How?
Screenshots:
After try&repeat, this did it for me:
int statusBarHeight = (int) Math.ceil(25 * getResources().getDisplayMetrics().density);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT,
statusBarHeight,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
It works on 4.2.2 and 4.4.2
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);
Note not rooting required
Can't you put your app in a notitlebar theme [shouldn't remove the navigation bar] and implement your own view on top that mimics the notification bar look as a container and the rest of the contents would be fragments ?

Android Create "Ghost" Image (Touch-through)

I have created a service which will overlay an image on top of the whole screen.
This part is the code which will overlay an image on top of the whole screen:
#Override public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
img = new ImageView(this);
img.setImageResource(R.drawable.image); // Image is .png format
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.CENTER;
params.x = 0;
params.y = 0;
windowManager.addView(img, params);
}
However, any space which the image covers in untouchable. I want the image to be just overlaid, without affecting any other components on the screen, similar to what this does:
How can I make the area beneath the image touchable (touch-through)?
Edit
It seems that the transparent space of the image fills a square, and covers the screen (FYI).
So my ultimate goal is, to make the image touch-through.
I have tried the popupWindow class, recommended by a few SO users, but its purpose does not fulfill my needs and does not work.
I found that WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY solves the issue.
Try
img.setClickable(false);
http://developer.android.com/reference/android/view/View.html#setClickable(boolean)
Try setting the param flags to:
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
I've tried this with success before. The last two will help send touch events to the views behind it.

Switching between TYPE_SYSTEM_ALERT AND TYPE_SYSTEM_OVERLAY , and capturing the touch events not working properly

I made a View in service which runs always on top of every application in android.
Initially, the behaviour of service is TYPE_SYSTEM_ALERT .
WindowManager.LayoutParams layparams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
layparams .gravity = Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL;
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(serviceView, layparams );
I am changing the behaviour of service at run time means switching between TYPE_SYSTEM_ALERT AND TYPE_SYSTEM_OVERLAY on touch. Switch take place when I touch outside the view parameters and when touch inside the view parameters.
layparams .type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
Using updateViewLayout(serviceView, layparams )
PROBLEM: Is when I touch outside of view , the first touch just activate TYPE_SYSTEM_OVERLAY and after then outside app works fine. When TYPE_SYSTEM_OVERLAY is working , after that when you touch within the view it will trigger the action of object behind the view not the action of the view. But when u once touch the view TYPE_SYSTEM_ALERT will be applied and now the View will behave normally .
Current Implementation : I have to touch twice to work that particular thing when switch take place between layout parameters.
use this one... works perfectly on ICS and above
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;//This one is necessary.
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
params.gravity = Gravity.TOP | Gravity.RIGHT;
params.x = 0;
params.y = -params.height;
windowManager.addView(tile, params);
you can do this by calling updateViewLayout() on instance of WindowManager class.
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
// create new layout params
WindowManager.LayoutParams newlayparams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
//update new layout
wm.updateViewLayout(serviceView, newlayparams);
Hope it help!

Display view above status bar?

I recently saw an image of an app that was capable of displaying a view above the status bar and was also able to cover it with a view.
I know you can get a view right below the status bar from a view with align parent top. But how would you get a view on top of the status bar??
Example
Disable the System Status Bar - Without Root
After two full days of searching through SO posts and reading the Android docs over and over.. Here is the solution that I came up with. (tested)
mView= new TextView(this);
mView.setText(".........................................................................");
mLP = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
100,
// Allows the view to be on top of the StatusBar
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
// Keeps the button presses from going to the background window
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// Enables the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
mLP.gravity = Gravity.TOP|Gravity.CENTER;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mView, mLP);
Dont forget the permissions:
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
Note:
Tested upto kitkat.
The answer by #Sadeshkumar is incorrect for ICS and above (perhaps GB as well).
A view created with TYPE_SYSTEM_ALERT and FLAG_LAYOUT_IN_SCREEN is covered by the StatusBar.
To get an overlay on top of the StatusBar, you need to use TYPE_SYSTEM_OVERLAY instead of TYPE_SYSTEM_ALERT.
The problem being then, how to get clicks/touches?
A view created with TYPE_SYSTEM_ERROR and FLAG_LAYOUT_IN_SCREEN is covered by the StatusBar.
int statusBarHeight = (int) Math.ceil(25 * getResources().getDisplayMetrics().density);
View statusBarView = new View(MyActivity.this);
statusBarView.setBackgroundColor(Color.GREEN);
WindowManager.LayoutParams params = null;
params = new WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT,statusBarHeight,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(statusBarView, params);

Categories

Resources