How to prevent from out touch in window manager? - android

I have a service from which I want to show a layout using a window manager which I successfully implemented but I need to do 2 things I want to access a button in layout and prevent from outside touch. How do I do that
final WindowManager windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
final RelativeLayout layout=(RelativeLayout) inflater.inflate(R.layout.sheet,null);
cancel = layout.findViewById(R.id.img_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
windowManager.removeViewImmediate(layout);
}
});
WindowManager.LayoutParams p = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.RGBA_8888);
p.gravity = Gravity.BOTTOM;
windowManager.addView(layout,p);

Add these flags
WindowManager.LayoutParams p = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? LayoutParams.TYPE_APPLICATION_OVERLAY : LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.RGBA_8888);

Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
Override onTouchEvent() of dialog
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
System.out.println("TOuch outside the dialog ******************** ");
}
return false;
}

Related

Window manager blocking keyboard for apps underneath floating chat head

I have an app in which i have implemented chat heads just like facebook messenger app It works fine but once keyboard is used by my floating chat head other apps cant open keyboard. I am using window manager to show floating view this is how i am setting my layout params in window manager
public static WindowManager.LayoutParams getWindowManagerOutsideRoom(){
final WindowManager.LayoutParams params;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL ,
PixelFormat.TRANSLUCENT);
} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL ,
PixelFormat.TRANSLUCENT);
}
params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
return params;
} ```
any help in this regard will me much appreciated
So I figure out it my self by reading Window manager flags, The solution was to use different Flags when you want touches to be handled and when not, Posting the snippet ``
public static WindowManager.LayoutParams enableKeyPadInput(Boolean enable) {
final WindowManager.LayoutParams params;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if (enable) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}
} else {
if (enable) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}
}
params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
params.gravity = Gravity.BOTTOM;
return params;
}
``

ImageView Click not working in android service Custom layout

ImageView click is not working on custom layout creatd in android service.
public class CallService extends Service {
String incomingnumber;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
incomingnumber = intent.getExtras().getString("incomingnumber");
System.out.println("=== onStartCommand incomingnumber : " + incomingnumber);
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onCreate() {
super.onCreate();
final Context context = getApplicationContext();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup mView = (ViewGroup) inflater.inflate(R.layout.testactivity, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
ImageView imgCallPick = mView.findViewById(R.id.imgCallPick);
ImageView imgCaller = mView.findViewById(R.id.imgCaller);
imgCaller.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("===== imgCaller");
return false;
}
});
wm.addView(mView, params);
} }
i just create custom layout in service, the layout contain a diff view but the Imageview click not working. is there any solution for that, then help me.
Thanks
From this answer: Android - ImageView On Click
Add in your xml layout:
android:clickable="true"
This issue is due to use of deprecated param
WindowManager.LayoutParams.TYPE_PHONE
For getting touch events
Use this if you want a colored background
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.RGB_565);
Use this if you want a transparent background
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSPARENT);
Everything else remains same :)

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

Fullscreen service in Android

I have made a service that puts black rectangle on top of every activity and the screen goes black except status/notification bar and navigation bar. My goal is to hide everything. How can I make this black rectangle go fullscreen? I have tried using FLAG_FULLSCREEN and FLAG_LAYOUT_IN_SCREEN, but it didn't work. I have read about disabling the TypeGuard, but even that didn't work. Thanks in advance!
Here is my code
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
ImageView black = new ImageView(this);
black.setImageResource(R.drawable.black);
LinearLayout layout = new LinearLayout(this);
layout.addView(black);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
windowManager.addView(layout, params);
i solved this issue just simply adding one line
layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
I solved this program
if (null == mInflater) {
mInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}if (mLockscreenView == null) mLockscreenView = mInflater.inflate(R.layout.view_locokscreen, null);
mLockscreenView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
mWindowManager.addView(mLockscreenView, mParams);
final Handler handler = new Handler();
mLockscreenView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
mLockscreenView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
},2000);
}
});
Try this:
assert getSupportActionBar() != null;
getSupportActionBar().hide();
mContentView = findViewById(R.id.fullscreen_content);
mContentView.setSystemUiVisibility(View.GONE | View.SYSTEM_UI_FLAG_FULLSCREEN);
Where fullscreen_content is the parent layout.

could not get my window manager screen above locked screen in android (without unlocking the device))

here is the service that i have called to display view above locked screen.i dont want to unlock lock screen i want something that whatsapp and skype use for their calling screen.such that screen is not unlocked.
public class Display extends Service {
private static WindowManager wm;
private static RelativeLayout ly;
private WindowManager.LayoutParams params;
#Override
public void onCreate() {/
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
LayoutInflater vi;
vi = LayoutInflater.from(this);
ly = (RelativeLayout) vi.inflate(R.layout.display, null);
wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
/*ALSO TRIED USING WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON*/
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.RGBA_8888);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 300;
ly.setLayoutParams(params);
TextView tv = (TextView) ly.findViewById(R.id.annotationTextView);
tv.setText("check");
wm.addView(ly, params);
return START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
}
}
but the screen was appearing below locked screen.
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
PixelFormat.RGBA_8888);
worked for me but i was not able to touch on it so still unsuccessful

Categories

Resources