Android Studio overlays - java

I want to start creating long running applications. For instance, you choose some settings and then they are used while you browsing or playing a game. For example shortcut apps. You click somewhere in the corner, and something pop ups, with list of applications you want to go to.
My problem: I don't know how to create buttons, which would be on phone screen, like triggers, when you touch, something happens. I heard it's called overlays, but I couldn't manage to find tutorials about it.
Application example: Pie Control on Google Play. https://play.google.com/store/apps/details?id=jun.ace.piecontrol
Thanks for helping me out.

use system alert window .
example
View mView = new View(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);

Related

Overlay of type "TYPE_ACCESSIBILITY_OVERLAY" hides navigation buttons

I'm adding a view to windows manager using these layout parameters:
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT
);
The expected result is an overlay window that covers the entire screen except the system ui (navigation bar and status bar) and doesn't cover the soft keyboard.
The above parameters do that but the problem is that the navigation bar buttons are not visible while the soft keyboard is open.
Current result:
Expected result:
I've tried a couple of flags like:
WindowManager.LayoutParams.FLAG_LAYOUT_ATTACHED_IN_DECOR
and a combination of:
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
But none of them worked.
I've also gone through the documentation for system ui but most option there that involve WindowManager.LayoutParams flags are for hiding the decoration and the rest of them require a reference to the window which I don't think I can get from an accessibility service.
The problem seems to be the:
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
flag, if I remove it, the navigation buttons are visible but the overlay will cover the keyboard, it will do that even if I use something like this:
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
My question:
Is there any way to have an overlay that doesn't cover the keyboard and doesn't hide the navigation bar buttons ?
Did you try switching from PixelFormat.TRANSLUCENT to PixelFormat.TRANSPARENT? Also:
.apply {
gravity = Gravity.BOTTOM //CENTER or whatever
x = 0
y = 0
screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}

Keyboard Not showing FLAG_NOT_TOUCH_MODAL

I'm creating a floating messaging function like the Messenger app.
I fixed keyboard not showing on a service by using FLAG_NOT_TOUCH_MODAL on my Layout params, but when the service starts, other apps like google,
search apps on phone, the keyboard is not working.
Here's the first picture - showing that the keyboard is showing: https://i.imgur.com/aTHUJm1.png.
Second picture - my service starts: https://imgur.com/C6PIGwk
Third picture - keyboard not showing anymore: https://imgur.com/hxJbEsX
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
the above code is my code for showing the keyboard on my service. Using the FLAG_NOT_TOUCH_MODAL shows the keyboard on my service, I tried replacing FLAG_NOT_TOUCH_MODAL with FLAG_LOCAL_FOCUS_MODE but still the problem persists.
This is a very old question, but the first result for the issue
The issue is the overlay is still taking focus away from any editable views. Combine FLAG_NOT_TOUCH_MODAL with FLAG_NOT_FOCUSABLE to fix this
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL ||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE

how can I right-align activity titles?

I'm new to android development and I need proper instructions to right-align activity titles in a min-sdk:8 android application. I can do so by enabling rtl support for my app, but that only takes effect when the user has chosen an rtl language on his/her device. is there a way to force rtl even when the device language is ltr? or any other instructions to right-align titles in ltr mode? thx.
You can set a custom view in your Toolbar like this:
(copied from How to align center the title or label in activity?)
TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.RIGHT );
customView.setText("Some centered text");
getSupportActionBar().setCustomView(customView, params);
You can do that^. If you were do that multiple times in your app, you can create a customToolbar class that extends Toolbar and overwrite this method and add all that code there^ (and use that custom toolbar in your xml as well)
https://developer.android.com/reference/android/widget/Toolbar.html#generateLayoutParams(android.util.AttributeSet)

How to create a system overlay in Android which allows interaction with the windows below it?

As seen in this app, I want to create an app which dims the screen by creating a shaded overlay.
The window is created, and it's partially transparent, however, I cannot get the applications below it to launch. I can click them, and see the button presses, but other apps cannot launch while mine is running.
Suggestions?
I enclosed my code below, and an example of an app which is already doing this.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup mTopView = (ViewGroup) inflater.inflate(R.layout.activity_black, null);
getWindow().setAttributes(params);
wm.addView(mTopView, params);
https://play.google.com/store/apps/details?id=com.haxor
This is an easy way:
Dialog dialog = new Dialog(this,android.R.style.Theme_DeviceDefault_Dialog_NoActionBar_MinWidth);
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
dialog.getWindow().setFormat(PixelFormat.TRANSLUCENT);
dialog.getWindow().setDimAmount(0);
dialog.setContentView(new EditText(this));
dialog.show();
Modified the Activity to be settings, and moved the blacking component to a service, which worked fine.

Having application running above other app

I want to make an activity that can be opened above ANY app.
Normally, even when the activity is set as dialog, when you switch to my app, you see my app, and in the background you see the launcher:
BUT, I want the app will go above any app like this: (made in photoshop):
I did see this question Creating a system overlay window (always on top), but in ICS there is no functionallity to the layout.
Furthermore, I want to give a dialog box from my app without minimizing the other app...
there are plenty of apps that show a floating view on top of everything like : airbrowser , LilyPad , Stick it , AirTerm , Smart Taskbar , aircalc ...
anyway , in order to achieve this feature , you must have a special permission called "android.permission.SYSTEM_ALERT_WINDOW" , and use something like that:
final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
final View view=findViewById(R.id.my_floating_view);
final ViewGroup parent=(ViewGroup)view.getParent();
if(parent!=null)
parent.removeView(view);
param.format=PixelFormat.RGBA_8888;
param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
param.gravity=Gravity.TOP|Gravity.LEFT;
param.width=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().width;
param.height=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().height;
final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wmgr.addView(view,param);
// TODO handle overlapping title bar and/or action bar
// TODO you must add logic to remove the view
// TODO you must use a special permission to use this method :android.permission.SYSTEM_ALERT_WINDOW
// TODO if you wish to let the view stay when leaving the app, make sure you have a foreground service running.
I'm one of the developers of the Tooleap SDK, and we also dealt with this issue.
Basically, you don't need to use the SYSTEM_ALERT_WINDOW to display an activity on top of another one. You can just display a regular "shrinked" Activity with a transparent background.
To make a "shrinked Activity, change the activity window layout params of height and width:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = ...;
params.y = ...;
params.width = ...;
params.height = ...;
this.getWindow().setAttributes(params);
To make a transparent background add to your activity definition in the manifest file:
android:theme="#android:style/Theme.Translucent"
That way, you can create the illusion of a floating activity:
Note that only the foreground activity will be resumed, while the background one is paused. But for most apps this shouldn't be an issue.
Now all that remains is when to launch the floating activity.
Here is an example of a "floating" calculator app using a regular activity. Note that the activity below the calculator belongs to another app.

Categories