I have created a popUpwindow which contains this properties:
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(false);
After mentioning this also there is a dismissal of popUpwindow when clicked outside the popUpWindow. Can anybody tell me what can be done to avoid this.
And also i have a music getting played in popUpwindow but when it plays i am not able to increase or decrease volume for the same.
I am not able to access media volume controls when there is popUpwindow in focus. I have read that if there is overidden method onKeydown / up then setVolumeControlsStream(AudioManager.Stream_Music) will not work. But i have used keyDown method to handle backpress rest i am returning return super.onKeyDown(keyCode, event);
Please suggest method to overcome this.
Try this
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);
if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
return false;
}else{
return super.dispatchTouchEvent(ev);
}
}
May this help you. i used this to avoid dismissal of Dialog when clicked outside the Dialog.
Try this-
put below line end of your class
#Override
public void onBackPressed() {
if (popupWindow != null)
popupWindow.dismiss();
else
super.onBackPressed();
finish();
}
Set focusable to false for popup window. It will avoid dismissal of popUpwindow when clicked outside the popUpWindow.
popupWindow.setFocusable(false);
Related
I'm creating a custom soft keyboard, and created a PopupWindow to show when a key is long pressed, like when you long press E and it shows E, É, È for you to choose one. The popup has a key to close him, but I want to remove this key and make him shows just while the user is touching, then the user long press, drag to the key that he want and release.
I'm using android API 8.
The popup is created in a KeyboardView class in the onLongPress method.
final View custom = LayoutInflater.from(context)
.inflate(R.layout.popup_layout, new FrameLayout(context));
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(custom);
popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popup.showAtLocation(this, Gravity.NO_GRAVITY, popupKey.x, popupKey.y-50);
The button for close the popup:
buttonCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
I think can use something with the onTouch method, but how to identify the event of releasing? And where use it? On the keyboardView, or maybe on the popup window?
I managed to do this with:
#Override
public boolean onTouchEvent(MotionEvent me){
if(popup != null && me.getAction() == MotionEvent.ACTION_UP){
popup.dismiss();
}
}
I first created a method to show on logcat the code of every touch event, then I got the code that appeared when I release the touch and compared with the documentation, and it it the code for MotionEvent.ADCTION_UP.
With this, it's just put a dismiss on the popup.
I saw that there is a private function called KeyboardView.dismissPopupKeyboard().
This is ugly, but because I am using the default PopupWindow (I didn't overrided OnLongPress()) and I saw thata KeyboardView.onClick() calls dismissPopupKeyboard() and that's it, what I did was:
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP){
getOnKeyboardActionListener().onKey('1', null);
dismissPopupKeyboard();
}
return true; // Required for recieving subsequent events (ACTION_MOVE, ACTION_UP)
}
I want to dismiss the pop up window when back button is pressed. I tried with this code:
popwindow.setBackgroundDrawable(new BitmapDrawable()); and it works. But in my app, pop should remain, even after touching outside of the pop up window. It should be dismissed only when back button is pressed. So I tried this: popwindow.setFocusable(false);
Now its not dismissing when touched outside the pop up. But its not dismissing on back press too. I do not want to overide `onBackPressed(). Is there any other way, through which i can achieve this. Thanks in advance..
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
new ScaleInAnimation(popupView).animate();
popupWindow.getContentView().setFocusableInTouchMode(true);
popupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
popupWindow.dismiss();
return true;
}
return false;
}
});
To disable pop up dismiss when click outside of popwindow
set
popwindow.setCanceledOnTouchOutside(false);
and for dismiss it on back button
set
popwindow.setCancelable(true);
Set like this..
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setTouchInterceptor(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (AppContext.isDebugMode())
Log.d("POPUP_WINDOW", "v: "+v.getTag() + " | event: "+event.getAction());
popupWindow.dismiss(); return true;
}
});
Aside from the setOutsideTouchable(true) and setFocusable(true) I had to add
popUpView.setBackgroundDrawable(new BitmapDrawable())
to make it work. This did not change the UI of my popup, but for some magical reason, enabled the back button functionality.
The code below may be helpfull:
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(mParent);
And,the show of the popupWindow (popupWindow.showAsDropDown or popupWindow.showAtLocation )must be called in the end.
Set Popup property as..
popup.setOutsideTouchable(false);
popup.setCancelable(true);
I have a PopupWindow with a Close-Button, and I only want to close the PopupWindow when this Close-Button is clicked. In addition, I don't want to underlying Activity-View to be affected by any touches.
This has been asked before: Android Popup Window dismisses when clicked outside. But since it's a question and answer of three years ago (2011) I was wondering if there is a better solution for this now or I should indeed use the accepted answer's method.
You need to do both:
popupWindow.setOutsideTouchable(false);
popupWindow.setFocusable(false);
Check this out
try this link OR one of the below code
pw.setOutsideTouchable(false);
OR
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);
if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
// Tapped outside so we finish the activity
this.finish();
}
return super.dispatchTouchEvent(ev);
}
Hope this helps...
I want to know how to close a pop_up window once the user clicks outside it, I had a look at PopupWindow - Dismiss when clicked outside but without any luck, and I tried that code:
pw.setBackgroundDrawable(null);
pw.setOutsideTouchable(true);
pw.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_OUTSIDE)
{
pw.dismiss();
return true;
}
return false;
}
});
Try this out.Hope it works :)
solution 1:
popupWindow.setFocusable(true);
popupWindow.update();
If this dont work.Then you can try this out.
solution 2:
You can use dialog.setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.
Something like,
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);
Or if your Dialog in non-model then,
1 - Set the flag-FLAG_NOT_TOUCH_MODAL for your dialog's window attribute
Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
2 - Add another flag to windows properties,, FLAG_WATCH_OUTSIDE_TOUCH - this one is for dialog to receive touch event outside its visible region.
3 - Override onTouchEvent() of dialog and check for action type. if the action type is 'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform. view plainprint?
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
System.out.println("TOuch outside the dialog ******************** ");
this.dismiss();
}
return false;
}
Ok so fixed in the end.
First made the main layout which the popup sits on a relative layout. Then placed a full screen blank layout on top which I made invisible and transparent.
Then show when the popup is shown, set the full screen panel visible with setVisibility(View.VISIBLE); and hide when popup is hidden with setVisibility(View.GONE);
Also need to return true from an on touch listener for the layout with (To stop touch events passing back to the main layout):
blocker.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
And give the popup window the properties:
setTouchable(true);
setOutsideTouchable(false);
Cheers
you should'nt set the BackgroundDrawable to null, because that kills the OnTouchListener ;
you should replace pw.setBackgroundDrawable(null); by pw.setBackgroundDrawable(new BitmapDrawable())
Better use a dialog Fragment for the same. It is made for the popup functionality and closes by default on pressing outside the dialog Fragment or using the hard back button.
I have implemented a custom dialog for my application. I want to implement that when the user clicks outside the dialog, the dialog will be dismissed.
What do I have to do for this?
You can use dialog.setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.
Something like,
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);
Or if your Dialog in non-model then,
1 - Set the flag-FLAG_NOT_TOUCH_MODAL for your dialog's window attribute
Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
2 - Add another flag to windows properties,, FLAG_WATCH_OUTSIDE_TOUCH - this one is for dialog to receive touch event outside its visible region.
3 - Override onTouchEvent() of dialog and check for action type. if the action type is
'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform.
view plainprint?
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
System.out.println("TOuch outside the dialog ******************** ");
this.dismiss();
}
return false;
}
For more info look at How to dismiss a custom dialog based on touch points? and
How to dismiss your non-modal dialog, when touched outside dialog region
Simply use
dialog.setCanceledOnTouchOutside(true);
You can use this implementation of onTouchEvent. It prevent from reacting underneath activity to the touch event (as mentioned howettl).
#Override
public boolean onTouchEvent ( MotionEvent event ) {
// I only care if the event is an UP action
if ( event.getAction () == MotionEvent.ACTION_UP ) {
// create a rect for storing the window rect
Rect r = new Rect ( 0, 0, 0, 0 );
// retrieve the windows rect
this.getWindow ().getDecorView ().getHitRect ( r );
// check if the event position is inside the window rect
boolean intersects = r.contains ( (int) event.getX (), (int) event.getY () );
// if the event is not inside then we can close the activity
if ( !intersects ) {
// close the activity
this.finish ();
// notify that we consumed this event
return true;
}
}
// let the system handle the event
return super.onTouchEvent ( event );
}
Source: http://blog.twimager.com/2010/08/closing-activity-by-touching-outside.html
Or, if you're customizing the dialog using a theme defined in your style xml, put this line in your theme:
<item name="android:windowCloseOnTouchOutside">true</item>
This method should completely avoid activities below the grey area retrieving click events.
Remove this line if you have it:
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
Put this on your activity created
getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
then override the touch event with this
#Override
public boolean onTouchEvent(MotionEvent ev)
{
if(MotionEvent.ACTION_DOWN == ev.getAction())
{
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);
if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
// You have clicked the grey area
displayYourDialog();
return false; // stop activity closing
}
}
// Touch events inside are fine.
return super.onTouchEvent(ev);
}
dialog.setCanceledOnTouchOutside(true);
to close dialog on touch outside.
And if you don't want to close on touch outside, use the code below:
dialog.setCanceledOnTouchOutside(false);
You can try this :-
AlterDialog alterdialog;
alertDialog.setCanceledOnTouchOutside(true);
or
alertDialog.setCancelable(true);
And if you have a AlterDialog.Builder Then you can try this:-
alertDialogBuilder.setCancelable(true);
This code is use for when use click on dialogbox that time hidesoftinput and when user click outer side of dialogbox that time both softinput and dialogbox are close.
dialog = new Dialog(act) {
#Override
public boolean onTouchEvent(MotionEvent event) {
// Tap anywhere to close dialog.
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);
if (!dialogBounds.contains((int) event.getX(),
(int) event.getY())) {
// You have clicked the grey area
InputMethodManager inputMethodManager = (InputMethodManager) act
.getSystemService(act.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(dialog
.getCurrentFocus().getWindowToken(), 0);
dialog.dismiss();
// stop activity closing
} else {
InputMethodManager inputMethodManager = (InputMethodManager) act
.getSystemService(act.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(dialog
.getCurrentFocus().getWindowToken(), 0);
}
return true;
}
};
Another solution, this code was taken from android source code of Window
You should just add these Two methods to your dialog source code.
#Override
public boolean onTouchEvent(MotionEvent event) {
if (isShowing() && (event.getAction() == MotionEvent.ACTION_DOWN
&& isOutOfBounds(getContext(), event) && getWindow().peekDecorView() != null)) {
hide();
}
return false;
}
private boolean isOutOfBounds(Context context, MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
final View decorView = getWindow().getDecorView();
return (x < -slop) || (y < -slop)
|| (x > (decorView.getWidth()+slop))
|| (y > (decorView.getHeight()+slop));
}
This solution doesnt have this problem :
This works great except that the activity underneath also reacts to the touch event. Is there some way to prevent this? – howettl
Following has worked for me:
myDialog.setCanceledOnTouchOutside(true);
Call dialog.setCancelable(false); from your activity/fragment.
You can make a background occupying all the screen size transparent and listen to the onClick event to dismiss it.
I tried some answers still I faced a problem like when I press outside the dialog dialog was hiding but a dimmed view was showing, and pressing again would go to the parent activity. But actually I wanted to go the parent activity after first click.
So what I did was
dialog.setOnCancelListener(this);
and changed my activity to implement DialogInterface.OnCancelListener with
#Override
public void onCancel(DialogInterface dialog) {
finish();
}
And boom, it worked.
Here is the code
dialog.getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent ev) {
if(MotionEvent.ACTION_DOWN == ev.getAction())
{
Rect dialogBounds = new Rect();
dialog. getWindow().getDecorView().getHitRect(dialogBounds);
if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
// You have clicked the grey area
UiUtils.hideKeyboard2(getActivity());
return false; // stop activity closing
}
}
getActivity().dispatchTouchEvent(ev);
return false;
}
});
Try this one . you can hide the keyboard when you touch outside