How to remove WindowManager view from screen? - android

I used
((WindowManager) context.getSystemService(Service.WINDOW_SERVICE)).removeView(view);
but it didn't work..view is still showing on the screen.
so how to remove WindowManager view from screen?
i Created it like this
public void create()
{
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10, context.getResources().getDisplayMetrics());
layoutParams.x = 265;
layoutParams.y = height;
layoutParams.format = PixelFormat.TRANSLUCENT;
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.ad_dialog, null);
Button bttnOk = (Button) view.findViewById(R.id.bttn_ok);
Button bttnCancel =(Button) view.findViewById(R.id.bttn_cancel);
bttnOk.setOnClickListener(this);
bttnCancel.setOnClickListener(this);
windowManager.addView(view, layoutParams);
}

windowManager.removeViewImmediate(view);
Make sure you added that view before or it will crash your app

Related

how to make overlay window touchable

i'm making a feature in my app that display an overlay window with text on the screen, when it appears then i try to touch that window it actually touches behind that window
i need to make that window touchable , which params should i use ?
here is my code
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater screenNotificationInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
layOverTopviewNotification = screenNotificationInflater.inflate(R.layout.layout_notification, null);
TextView layOverText = (TextView) layOverTopviewNotification.findViewById(R.id.notification_layout);
layOverText.setText(athkarString[completed]);
params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.x = 0;
params.y = 0;
params.gravity = Gravity.TOP;
windowManager.addView(layOverTopviewNotification, params);
I finally found an answer.
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
inflater = LayoutInflater.from(PlayAthkarService.this);
textView = new TextView(PlayAthkarService.this);
textView.setBackgroundResource(R.drawable.floating_window);
textView.setTextSize(20);
textView.setText(athkarString[completed]);
textView.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.OPAQUE);
}else {
params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
, WindowManager.LayoutParams.TYPE_PRIORITY_PHONE,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.OPAQUE);
}
params.gravity = Gravity.TOP | Gravity.RIGHT ;
params.x = 0;
params.y = 50;
windowManager.addView(textView, params);
Maybe you can try to set textview a touchListener and make textview full screen

Android : How to set onCick listener to custom layout which is displayed using the WINDOW_SERVICE as floating window?

In service for displaying custom view over all windows I need to set onClick listener.
#Override public void onCreate() {
super.onCreate();
mContext = this;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.result_layout,
null);
// Here i need to add onClick listener
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(parent, params);
}
I tried to create button lister from inflater but without luck. What is the right approach to set click event listener for selected button by button ID?
Many thanks for any advice.
Resolved using the following code which is works in the service well:
#Override public void onCreate() {
super.onCreate();
mContext = this;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mParentLayout = (LinearLayout) inflater.inflate(R.layout.result_layout,
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.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(mParentLayout, params);
Button b = (Button) mParentLayout.findViewById(R.id.closeButton);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
windowManager.removeView(mParentLayout);
}
});
}
Inspired from:
Could not get Touch event for TYPE_SYSTEM_OVERLAY
Android can play tricks on what you think might work, but doesn't.
First, What I typically do is from onCreate() I place a call to:
InitializePage(); where I then put all my initialize code. It's just my style.
Then in my InitializePage() function I initialize the button, then enter the OnClickListener. I use Android Studio with Intellisense so it writes the code for me....
mSendButton = (TextView)findViewById(R.id.send_button1);
mSendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{.....
});
It should be that easy. Good luck

Set background then inflate layout

I want to inflate a layout to view and then add it to WindowManager but before I want to make background of layout to be the same as wallpaper. I've tried this code but sadly WindowManager didn't show my new background.Please, help me!!!
My code:
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
RelativeLayout rl = (RelativeLayout) findViewById(R.id.main);
rl.setBackgroundDrawable(wallpaperDrawable);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup mTopView = (ViewGroup) inflater.inflate(R.layout.activity_main, null, true);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(params);
wm.addView(mTopView, params);

Selection text in WebView which created by service

How can longclick to select a text in WebView in a Service. I added a WebView by the WindowManager, when a url is loaded and a longclick to select text is done. The selected text is not visible and can not be selected.
How can I fix it ?
public class BtService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
public static final int WINDOW_EXPAND_FLAGS = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_DIM_BEHIND
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
#Override
public void onCreate() {
super.onCreate();
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WINDOW_EXPAND_FLAGS, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
LinearLayout view = new LinearLayout(this);
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
WebView wv = new WebView(this);
wv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
view.addView(wv);
wv.loadUrl("https://tinhte.vn");
windowManager.addView(view, params);
}}
Also this will require the android.permission.SYSTEM_ALERT_WINDOW permission.
When webview loaded, longclick in text to select text,-> action mode don't show

show text over lock screen

How can I show TextView and EditView over the lock screen?
I've tried this and I also add TextView in the layout but nothing has happened
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
then I tried this (in Service) and it didn't work too
#Override
public void onCreate() {
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| PixelFormat.TRANSLUCENT);
TextView textView = new TextView(this);
textView.setId('1');
textView.setText("This is a demo");
textView.setTextSize(15);
textView.setGravity(Gravity.CENTER);
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(textView, mLayoutParams);
}
Help me please
you need to do it like this:
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
80, 80, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.CENTER;
params.setTitle("text params");
wm.addView(YourTextView, params);

Categories

Resources