How to delegate touch events on both popup window and activity android - android

I have an activity which is having two buttons and a popup window.By click a button popup will display. I want to handle touch events in both popup window and activity.
In the below image i want to handle Ok button click with out dismiss the popup widow. At the same time i need dismiss button event as well.
I have tried
popWindow.setTouchable(true); then activity events are not working.
popWindow.setTouchable(false); then popup window events are not working.
Please see the image for reference.
Thanks in advance.

You can achieve this using WindowManager.
private void popUp(View yourPopUpView,WindowManager.LayoutParams params){
WindowManager manager = (WindowManager)getSystemService(WINDOW_SERVICE);
manager.addView(yourPopUpView,params);
}
remove same view using
private void removePopUp(View yourPopUpView){
WindowManager manager = (WindowManager)getSystemService(WINDOW_SERVICE);
manager.removeView(yourPopUpView);
}
but don't forget to remove that view onResume of your activity. B'coz it will remain after destroy of your activity also..
Hope this helps...

Related

Can a custom view get something like "onClose" or "onDestroy" event?

I have created a custom compound view by extending FrameLayout. That view can be on a dialogue box (among others). But what if I want to do some clean-up work when the view disappears from the screen, such as the user's closing the dialogue box? Can I get some kind of onDestroy event? Or should I make the owner (such as the dialogue box or the fragment) call the view's clean-up method on its (owner's) onDestroy or dismissed event?
try to use
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}

Keyboard and Focus issue in PopupWindow

I am showing a popup window , if i initialize it as follows :
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(false);
And
#Override
public void onBackPressed() {
if (popupWindow.isShowing()) {
showConfirmationDialogForExit();
} else {
super.onBackPressed();
}
}
The Keyboard in my PopupWindow doesn't open ( the popupWindow has EditText ).
Also i have kept a dialog box to confirm the exit of the screen on Back Press which works.
Now opposite to this , if i keep
popupWindow.setFocusable(true);
The keyboard opens and works but the onBackPressed() doesn't check the
popupWindow.isShowing()
here my question is , i want to achieve the backpress check when the popup window is showing and also the keyboard needs to be worked when click on the EditText on that popupWindow.
The solutions i have gone through suggests me to keep
popupWindow.setFocusable(true);
but i am not be able to achieve the goals i am looking for.
Please guide me.
try
popWindow.setFocusable(true);
popWindow.update();
This updates the state of the popup window, if it is currently being displayed, from the currently set state.
If that don't work for you for some reason, you can always just set a local flag when you open the popWindow....
boolean popupshowing = true;
And then use that to check if it is open or not. (I agree it isn't the most elegant solution)

Android - Memory leak with popup window in Recyclerview

I'm having trouble with the following error displaying in my app:
"E/WindowManager: android.view.WindowLeaked: Activity com.awt.myapp.MyList has leaked window android.widget.PopupWindow$PopupDecorView{84fdb1f V.E...... .......D 0,0-369,120} that was originally added here..."
Basically I've got a recyclerview and in the adapter I have a bunch of textviews in each row and am binding click listeners to them, as clicking one of these textviews brings up a popup window. The problem is if I hit the Android back button while a popup is still visible the above error appears.
I understand that in my activity that holds the recyclerview I can add an 'onBackPressed()' method, but from here I'm not sure how to get a reference to any of the popup windows within the adapter (and close it at this stage) as I believe this is what I need to do.
Below is my click listener code, I've experimented with some options and having setFocusable just causes the back button to stop working so not sure if that's needed.
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View moreInfoView) {
myPopupWindow.setBackgroundDrawable(new ColorDrawable());
//myPopupWindow.setFocusable(true);
myPopupWindow.setTouchable(false); // Ignores taps
myPopupWindow.setOutsideTouchable(true); // Disappear when tapping anywhere on screen
int position = -tv.getHeight();
myPopupWindow.showAsDropDown(tv, 0, position);
((MyList) context).onToggleMoreInfo("show");
myPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
((MyList) context).onToggleMoreInfo("hide");
}
});
}
});
Hopefully this makes sense, if you need any more info let me know. Any advice would be appreciated.
If you create a listener on your adapter that implements the activity and calls it when you click on the item, you can export popup window logic to activity and override on back pressed to dimiss it.

Making a click-through Activity

I'm trying to adjust the brightness of my phone programmatically. I've got an Activity in my app which is translucent to do this, however users can't click through it to the activity/window beneath it. I kill this activity as soon as the brightness is set, however I want to do more work in this activity (such as gradual brightness adjustment) which requires some time, and I don't want the user tapping at their phone wondering why their actions aren't being registered.
So basically, I need to either create a mock Window which will successfully allow me to adjust screen brightness without being displayed, or work out how to make an Activity click-through. I'm not sure how to do either.
BTW, this is the code making the brightness adjustments in the Activity:
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, Math.round(SOME_BRIGHTNESS * 255f));
Window window = getWindow();
window.getAttributes().screenBrightness = SOME_BRIGHTNESS;
window.setAttributes(window.getAttributes());
float sysBrightPer = getSystemBrightness(getApplicationContext());
new Thread() {
public void run() {
try {
sleep(BRIGHT_TIMEOUT);
} catch (InterruptedException e) {
e.printStackTrace();
}
finish();
}
}.start();
Any advice?
P.S. I found this app on the market. I wonder if the way this has been achieved would help me? https://market.android.com/details?id=com.haxor
Hah! That app did help me, if only because it led me to this solution!
Brightness Screen Filter
For the click lazy, use this:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
I'm afraid this no longer works in 4.0+, if applied to the window itself (presumably to the prevent use of activity class methods like onkey from a transparent overlay). However one can still use windowmanager.addview or layoutinflater.inflate to add a custom view class which extends viewgroup, and if you use layoutparams.FLAG_NOT_TOUCHABLE in adding or inflating this view, it will be click-through, and remain on top even when the activity that called it goes into onpause.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
If you want to intercept touches on specific views you can use the window manager to add a view with different flags:
e.g.
WindowManager.LayoutParams params = new WindowManager.LayoutParams(...);
getWindowManager().addView(myButton,params);

Opening a context menu without registering the view

How can I open a context menu for a view without registering it using registerForContextMenu()?
My activity overrides onTouchEvent to detect motion, and if I use registerForContextMenu() it stops working. I'm detecting a long press, so I'd like to forward that request to a method that would build the menu for me, but don't know if it can work that way.
I believe you can use View.setOnCreateContextMenuListener to manually bind your activity as the context menu provider for a view at layout initialization time, and then call View.showContextMenu on the view once you detect a long press to bring up the menu. It should call through to your onCreateContextMenu call just as if you'd registered it the normal way.
EDIT: Okay, this has the same effect of overriding the touch event listener for the view. Given that, the only solution I can think of is to create a hidden view and register to be that view's context menu provider, and then ask it to show a context menu as above when a long touch on the real/visible view is seen.
My onTouchEvent have the followed lines. It's not beautiful, but it's working.
private void onLongPressed(View view) {
registerForContextMenu(view);
view.getParent().showContextMenuForChild(view);
unregisterForContextMenu(view);
}
The onLongPressed-function is calling, if I detect a long press in my one onTouchListener.

Categories

Resources