How to add a floating view to Android Window manager and listen to System/Hardware back button events
Asked Answered
L

5

12

I have a service which displays a floating view on the window manager (using WINDOW_TYPE_ALERT permission). I'm able to display it and perform actions. But, I have two specific questions:

  1. Regarding the implementation of the floating view
  2. How to listen to system back button event so that I can dismiss the view.

Implementation:

In the manifest I added permissions for:

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

I have a broadcast receiver which will listen for Alarm events. Upon receiving the event, I'm starting a service to display the floating view. The following is the code I'm using to create the view.

LayoutParams layOutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);

Whenever a user performs any action on the view, I'm removing the view from window manager and killing the service.

What I would like to know is - if this is the right way to approach the problem or are there any better ways to do it? And, should I make changes to the LayoutParams or keep them as is?

Secondly, I would also like to dismiss this floating view when there is SYSTEM BACK/HARDWARE BACK button press event. Any pointers on how to do this would be helpful.

Attaching a screenshot of the floating view for better understanding:

Floating View

Lowestoft answered 8/8, 2013 at 10:51 Comment(2)
just show a dialog, it will handle events for you #5469505Willette
@veon can you please tell me where to add the dialog?Lowestoft
K
11

In terms of back button detection - I made it to work in a following way (everything happens in service onCreate code):

  1. Wrap your desired view into ViewGroup (LinearLayout, Relative or other)
  2. override dispatchKeyEvent like this in wrapper view:

mView = new RelativeLayout(this) {
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
                // < your action >
                return true;
            }
            return super.dispatchKeyEvent(event);
        }
};
  1. add wrapper view to the window manager, and be sure that WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE is not set on wrapper layout params.
Kerseymere answered 15/9, 2014 at 10:54 Comment(1)
How did you get this to work? I can't touch anything / scroll.Povertystricken
I
6

Have a look at Standout Library , which is good for handling floating windows , it seems it is also not handling back press event , contacting developer might help.

And one more way is you can try opening activity with semi transparent background/theme to get the similar effect used in floating window in Any.do and backpress event can be handled

Inconsolable answered 4/9, 2013 at 13:29 Comment(1)
Thanks for the answer, you definitely pointed me in the right direction. To get this completely working exactly like Any.do I had to do the following: We have to launch the activity with launch mode "singleInstance" . We have to mention it in manifest file like this android:launchMode="singleInstance"Lowestoft
R
1

Do you want the HOME button to also dismiss your UI? If you do, it sounds like it is better to have an activity that opens on a transparent background, instead of an alert window. To do this use the following style as the theme for your activity

<style name="Transparent">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowFullscreen">true</item>
</style>
Racket answered 6/9, 2013 at 8:35 Comment(0)
T
1

Regarding the Back button - You should override the "onBackPressed()" inside your view and do whatever you want

@Override
public boolean onBackPressed() {
    // Remove your view from the window...
}

Anyway, I'm using an SDK called Tooleap, to display floating windows in a straight-forward way.

Trevelyan answered 23/6, 2014 at 9:11 Comment(1)
onBackPressed() is not a View method and so cannot be overridden. You can however override dispatchKeyEvent(KeyEvent event) and look for back presses.Apophyllite
G
-1

FOR WORKING "onBackpressed" BUTTON FOLLOW BELOW INSTRUCTION

1.Go to FlotingFolder.java file in example (Not library).

2.Find below method

@Override
public int getFlags(int id) {
    if (APP_SELECTOR_ID == id) {
        return super.getFlags(id);
    } else {
        return super.getFlags(id) | StandOutFlags.FLAG_BODY_MOVE_ENABLE
                | StandOutFlags.FLAG_WINDOW_EDGE_LIMITS_ENABLE
                | StandOutFlags.FLAG_WINDOW_FOCUSABLE_DISABLE;
    }
}
  1. Then remove - "StandOutFlags.FLAG_WINDOW_FOCUSABLE_DISABLE" from the above method

Now onBackPressed will work.

Galvanoscope answered 1/4, 2015 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.