Android WindowManager Window does not reposition after keyboard is visible - android

A window that is shows on screen
public class FlatingViewService extends Service{
public static WindowManager windowManager;
...
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
...
//Floating View Layout
li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
myView = li.inflate(R.layout.product_response_card, null);
name = (TextView) myView.findViewById(R.id.name);
editTextName = (TextView) myView.findViewById(R.id.editTextName);
...
//Layout Parameters
...
response_card_params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
When user tries to enter some text into editText, Keyboard appears but on top of editText. So the user can't see anything.
What's the actual problem?

Got the problem solved.
//Layout Parameters for Product response card
layout_params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_DIM_BEHIND,
PixelFormat.TRANSLUCENT);
layout_params.gravity = Gravity.LEFT;
layout_params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
I was using WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, instead of WindowManager.LayoutParams.TYPE_PHONE
And WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN

Starting from API 19 options SOFT_INPUT_ADJUST_PAN and SOFT_INPUT_ADJUST_RESIZE don't affect windows with types TYPE_SYSTEM_ALERT and TYPE_TOAST (and TYPE_VOLUME_OVERLAY for API 21 and 22; in API 23 TYPE_VOLUME_OVERLAY removed from this list).
Instead of use TYPE_SYSTEM_ALERT need use TYPE_PHONE.

Related

How to add LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES in Window Manager Params?

I am creating an Android App in which I am showing a floating view on top of all apps by using a service and Window Manager. I am trying to add WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES in my Window Manager params to enable my layout to overlay with display cutouts. But it seems like this is not working in my case.
I have already tried to add it in both ways:
By using XML styling:
<style name="OverlayTheme">
<item name="android:windowLayoutInDisplayCutoutMode">
shortEdges <!-- default, shortEdges, never -->
</item>
</style>
And added this style in my layout root view.
But it does not work.
By using the JAVA code in my service:
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT);
It also not working. I am not sure if I am applying the Layout Params Correctly or not. Please help me to figure out the problem. Thanks in advance.
Edit:
This is the way I had implemented my the bubble in my service:
Initialized Window Manager and Layout in onCreate() method:
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (inflater != null) mLayout = inflater.inflate(R.layout.my_layout, null, false);
Added this view to window by Window Manager:
WindowManager.LayoutParams params;
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT);
Finally added this view to Window by addView Method:
windowManager.addView(mLayout, params);
By using the above methods I am able to show my view on top of other apps. But the problem is that my view is not not overlapping with a display cutout. This is happening when android is in landscape mode.
Solution:
I solved this by just adding this line of code (As suggested in Answere):
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
Try setting in your Activity:
window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES

How to change Screen overlay after system ui mode changed(Fullscreen, hide navigation)?

I create overlay:
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*The parent view (actually the root view) the will be added in front of all the windows on the screen*/
mOverlayView = (RelativeLayout) inflater.inflate(R.layout.overlay_control, null);
mWindowManager.getDefaultDisplay().getSize(size);
mWindowSize = new Size(size.x, size.y);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
mWindowManager.addView(mOverlayView, params);
I need to move overlay if system UI Visibility changed. There is possibility to detect it(documentation) for view but it not works for overlay. So I only need callback to detect if mode of device changed.
I found the way to do it:
In Android there is OnSystemUiVisibilityChangeListener but it not works if there is not view which will has match_parent value of width or hight.
So I created such overlay and made it 1 px height and match_parent width and add OnSystemUiVisibilityChangeListener to it.

Android Disply TextView on top of screen whenever copied text from the any social media or any mail or any kind of EditText

I got the value of the copied text like copied from the any messenger or mail or other social media,like i write in messenger hello,i, copied that now i display that in toast message( all code in service so it works in backgrounds),it done, now i want to display this value in Textview on top of the screen, whenever i copied text from the media ,one textbox automatically opend on top of the screen and display my copied value, how to create this type of textbox,open on top of screen when every time copied value ? i need quick help,so please help me, thanks in advance.
You can do the following to add a TextView to the top of the screen
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
TextView view = (View) inflater.inflate(R.layout.activity_main, null);
view.setText(your_clipboard_text);
view.setBackgroundColor(Color.WHITE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
windowManager.addView(view, params);
R.layout.activity_main is any layout that you want to show, in your case just a TextView in an XML
You will also need to add the permission <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> in your manifest.
get windowManager in your service.
Add a TextView with your copied text on window.
Now you will have to take care to remove that view from window when done.
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_u_wish, flag, PixelFormat.*);
windowManager.addView(view, layoutParams);
Remove-
windowManager.removeView(view);

How to overlay a crosshair

I have seen many applications where the user touches the screen and the application displays an overlay crosshair view of the point. I would please like to know how this works and how to overlay it such as in the developer option in the phone, where it overlays every screen. Thank you.
If you want overlay everything, not just your application, call code like this from service.
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.whatToShow, null);
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.LEFT;
params.x = 0;
params.y = 0;
windowManager.addView(mView, params);
And to remove it
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
It requires following line in manifest:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Also check out this tutorial
http://www.piwai.info/chatheads-basics/
You can inflate a layout as a View and add view to the root view.
inflater = LayoutInflater.from(getBaseContext());
View view = inflater.inflate(R.layout.overlay, null);
LayoutParams layoutParamsControl= new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(view, layoutParamsControl);

How to make a Facebook Messenger-like notification like this in Android

I want to implement notifications like the one in the following image.
Notification appears any time. I think it's of course a background service waiting for new messages from the server then shows this. What I think this is an activity implemented as dialog with this custom UI. Am I correct? And is it a normal startActivity method from the service? And how do I do the transition animation to make it appears slowly from left to right with zooming when show up?
Check out this link http://www.piwai.info/chatheads-basics. He provides information about how to add them on your screen.
The trick is to add a View to the WindowManager like following code
private WindowManager windowManager;
private ImageView chatHead;
public void addView()
{
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.android_head);
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.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(chatHead, params);
}
Don't forget to add the permission <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Categories

Resources