Android - How do you make a button on a screen overlay clickable - java

I am trying to implement a simple lockscreen through screen overlay. I added a button to the XML layout, the app successfully covered the screen but the button doesn't seem to work.
public WindowManager winManager;
public RelativeLayout wrapperView;
#Override
protected void onCreate(Bundle savedInstanceState). { ((KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock("IN").disableKeyguard();
PackageManager p = getPackageManager();
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
finish();
super.onCreate(savedInstanceState);
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
this.winManager = ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE));
this.wrapperView = new RelativeLayout(getBaseContext());
getWindow().setAttributes(localLayoutParams);
View.inflate(this, R.layout.lackiscreen, this.wrapperView);
this.winManager.addView(this.wrapperView, localLayoutParams);
}
public void dclick(View view){
this.winManager.removeView(this.wrapperView);
this.wrapperView.removeAllViews();
}

Android only allows very specific windows to receive touch events when they are an overlay. You need to get your flags right.
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
Hope that helps. (Works for me at least)

Related

I want to go back to my fragment screen after clicking "textview" on my "floatingservice" class

public class FloatingViewService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {return null;}
#Override
public void onCreate() {
super.onCreate();
mFloatingView =
LayoutInflater.from(this).inflate(R.layout.homescree_layout_one,
null);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
layout_parms,
// WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.CENTER | Gravity.BOTTOM;
params.x = 0;
params.y = 100;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);
textviewApply = mFloatingView.findViewById(R.id.textviewApply);
textviewApply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Your theme applied successfully!", Toast.LENGTH_LONG).show();
}});
Here, textviewApply is used for setting the floating window.
I want to go back to my fragment screen after clicking textview on the floatingservice. How can I accomplish this?
This is a classic case that is good to use Broadcast Receiver:
Here a good example to build a custom broadcast receiver:
https://www.journaldev.com/10356/android-broadcastreceiver-example-tutorial
And cast if you want to learn more about the subject:
https://developer.android.com/guide/components/broadcasts

android - dynamically adding a button to a custom dialogbox

I am writing an android app,
I have an activity, inside it i have a button, and it's on click listener opens a dialog box from a custom XML using the following code:
I would like to add that dialog box another button that is not set in it's XML file.
All the components are excising in the XML and working just fine, the dialog opens but I can't add the b button.
this.addCheer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog d = new Dialog(map.this);
LinearLayout layout = findViewById(R.id.dialog_layout_root);
LayoutInflater layoutInflater = d.getLayoutInflater();
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
final EditText title = d.findViewById(R.id.cheerDialogText);
ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);
Button b = new Button(d.getContext());
b.setText("yo");
cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
I tried to use this example but it does not work for me. What am i doing wrong here?
Thanks!
Just try below code
this.addCheer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog d = new Dialog(map.this);
LayoutInflater layoutInflater = d.getLayoutInflater();
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.cheer_dialog);// custom layout for the dialog
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
LinearLayout layout = d.findViewById(R.id.dialog_layout_root);
final EditText title = d.findViewById(R.id.cheerDialogText);
ImageButton addCheerOk = (ImageButton) d.findViewById(R.id.addCheerOk);
Button b = new Button(d.getContext());
b.setText("yo");
layout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
You were using only findViewById(R.id.dialog_layout_root); instead of d.findViewById(R.id.dialog_layout_root);
Here ll_button is the id of layout which contains the two xml buttons you have.
LinearLayout ll_button = d.findViewById(R.id.ll_button);
LinearLayout.LayoutParams layoutParams = new inearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll_button.addView(b, layoutParams);
Remove your last line.
cheerDialogLayout.addView(b, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

How can I add a copy of my SupportMapFragment to my WindowManager to show it as a screen overlay?

I have an Android app that uses a screen overlay to place buttons on top of Pokemon Go. The buttons call methods to draw polygons on the map in the background. I have the buttons working how I want them to, but what I really want is to create an overlay onto of Pokemon go that shows my map so the user doesn't have to switch back and forth. (I just uploaded a version with the working buttons if you want to see that: https://play.google.com/store/apps/details?id=kavorka.venn_tracker).
I cant seem to get a copy of my SupportMapFragment into a view in my overlay, does anyone know a way to get this to work?
I am using a MapWrapperLayout so I can have a custom map info window with buttons (I don't know if there is a better way).
Here is the code that is called when I start the overlay:
Binder binder = new Binder();
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.width = 150;//mButtonCircleGreen.getLayoutParams().width;
params.height = 150;//mButtonCircleGreen.getLayoutParams().height;
params.token = binder;
WindowManager.LayoutParams greenParams = new WindowManager.LayoutParams();
WindowManager.LayoutParams redParams = new WindowManager.LayoutParams();
greenParams.copyFrom(params);
redParams.copyFrom(params);
greenParams.x = mSharedPref.getInt("overlay_green_x", (int) mButtonCircleGreen.getX());
greenParams.y = mSharedPref.getInt("overlay_green_y", (int) mButtonCircleGreen.getY());
redParams.x = mSharedPref.getInt("overlay_red_x", (int) mButtonCircleRed.getX());
redParams.y = mSharedPref.getInt("overlay_red_y", (int) mButtonCircleRed.getY());
topLeftView = new View(this);
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftParams.x = 0;
topLeftParams.y = 0;
topLeftParams.width = 0;
topLeftParams.height = 0;
topLeftParams.token = binder;
WindowManager.LayoutParams mapParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
mapParams.gravity = Gravity.CENTER | Gravity.CENTER;
mapParams.x = 0;
mapParams.y = 0;
mapParams.width = 1000;
mapParams.height = 1000;
mapParams.token = binder;
wm.addView(topLeftView, topLeftParams);
wm.addView(mMapView, mapParams);
wm.addView(mOverlayedButtonGreen, greenParams);
wm.addView(mOverlayedButtonRed, redParams);
Here is where I get my map:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this);
mMapView = new MapView(this);
mMapView.getMapAsync(this);
// Window manager for overlay
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Any ideas would be greatly appreciated :)

WindowManager - drawing on top of the navigation bar (buttons) Android

Good afternoon. I have a service in which the object is created and WindowManager View - is assigned parameter background (mView.setBackgroundColor(color). The fact is that now I have View drawn only over the main desktop screen and status bar, and navigation bar is not affected. How do I do to View draws and on top of the navigation bar (buttons) in the Android? At the moment:
#Override
public void onCreate() {
super.onCreate();
mView = new LinearLayout(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);
}
Result:
enter image description here
That is all I have to paint in red, relatively speaking, including a navigation bar. Thank you.
int value_below_screen=100;
params.y=-value_below_screen;
this will offset the screen below by "value_below_screen"

How to make clickable overlay activity android? [duplicate]

I'm developing lock screen. And I need my lock screen activity be over all apps.
So, I have this:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View oView = layoutInflater.inflate(R.layout.lock_screen, null); // lock_screen is .xml file
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(oView, params);
It works, but not at all. Everything is frozen, the navigation baris working, but touch screen is not. How can I make only my activity be over everything and can't quit it?
This is because you've only overlaid a view, not the activity.

Categories