How to dismiss PopupWindow when clicked outside? - android

In one of my activity, there is a button, which when clicked opens a PopupWindow.
My intention is, when the PopupWindow is opened and user clicks anywhere on screen except the popup area, the PopupWindow should be dismissed.
For this I have set:
// onClick()
myPopupWindow.setOutsideTouchable(true);
myPopupWindow.setFocusable(false);
Issue is it works fine & PopupWindow gets dismissed when I click anywhere outside, but if I click on the button that generated this PopupWindow, then that event is consumed and PopupWindow first gets closed and then gets opened again.
I tried moving my button onclick() code to onTouch().
But if I return true, then button consumes every event & opens popup again and again, even with slightest drag while touching the screen.
If I return false, it behaves same as in onClick() & opens the popup again when button is touches back.
So how can I dismiss the PopupWindow even when clicked on the button?

Just disable that button after it has been clicked so that it can't be clicked when the pop-up window is displaying. button.setEnabled(false);

I solved a similar problem as follows:
settingsImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!settingsMenu.isShowing()) {
settingsMenu.showAsDropDown(settingsImageButton);
settingsImageButton.setEnabled(false);
}
}
});
settingsMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
settingsImageButton.setEnabled(true);
}
}.execute();
}
});
I call the method setEnabled() asynchronously, because onDismiss event is earlier than onClick event.

Related

How to dismiss the parent custom dialog when clicking Ok on the second dialog

I am showing two dialogs each should be displayed with different network call on the same activity (Login Activity). In which if I click on "Resend Email" text view in the first dialog then I am having another network call that shows me another dialog. When I click "OK" on the second dialog, it is dismissed. But the first one is still shown. So how to dismiss both when I click "Ok" on the second.
create a local Dialog variable dialogOne and when you click on ok of the dialog two dismiss both
btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isShown = true;
dialog.dismiss();
dialogOne.dismiss();
}
});
Dismiss both dialog when button clicked and before dismiss must check dialog is visible or not to avoid nullpointer exception.
btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(dialog.isShowing())
dialog.dismiss();
if(firstdialog.isShowing())
firstdialog.dismiss();
}
});

Avoid AlertDialog to be dismissed when back button is pressed

I have an AlertDialog that shouldn't close when a certain condition, (a button of it isn't enabled) happens if the back button of the device is pressed.
With the following code, I've managed to partially achieve the desired behavior.
dialog1.setOnCancelListener(new DialogInterface.OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog)
{
Button button3 = ((AlertDialog)
dialog1).getButton(AlertDialog.BUTTON_NEUTRAL);
if (!button3.isEnabled())
{
dialog1.show();
}
else
{
dialog1.dismiss();
}
}
});
But this code presents 2 problems:
1) There's a small time where dialog1 stops showing to show again, this looks a bit bad.
2) Much more important, one needed button that displays when that button is disabled stops showing, this button doesn't initially show with the dialog, under some circumstance which also triggers that the shown button gets disabled it gets to show. For some reason, it looks like the dialog isn't refreshed to its last state and keeps only the elements that originally had.
Is there anyway so that if the back button is pressed when showing the dialog under the mentioned condition absolutely nothing happens or at the very least to keep the exact same elements it had when dismissed and is later shown again?
Use setCancelable();
Sets whether the dialog is cancelable or not. Default is true.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
this will not let you click outside the dialog to dismiss it, or simply go back closing it
dialog1.setCancelable(false);
Override the onBackPressed() and add check for button disabled:
#Override
public void onBackPressed() {
if (button3.isEnabled()) {
//do something
} else {
super.onBackPressed();
}
}

Android Long Click vs. Default Click

I have an EditText that is focused and a Button that is not focused.
When I click the Button, the EditText loses focus.
When I long-click the Button, the EditText does not lose focus.
Whats the source of this behaviour? I want to achieve the long-click behaviour within a default click, is this possible?
Long click behavior is for by default for ClipBoard actions . If you want to override it with Single click . You can do this as follows.
editText=(EditText)findViewById(R.id.txt);
editText.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// Do your stuff
return true;
}
});
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.performLongClick();
}
});
If you are doing it to just get rid of Focusing problem then its not the way.

Why doesn't a button's onClickListener fire when a PopupWindow is showing over it's window?

When a PopupWindow is showing, clicking a button outside of PopupWindow's area only dismisses the PopupWindow, but the button's click listener doesn't respond. My question is, why doesn't the button's click listener respond?
private OnClickListener mSiftClickListener = new
View.OnClickListener() {
#Override
public void onClick(View v) {
if (mSiftPopwin != null && mSiftPopwin.isShowing()) {
ToastShow.makeText(mContext, "yes"); //never show
} else {
showSfitPopwin();
}
}
};
My onClickListener has two functions, showSiftPopwin and ToastShow, when Popwin is showing, click it again, Popwin dismiss but this onClick listener has't been invoked.
This is the expected behaviour.
A popup window will consume any touch events on the screen until it is dismissed. The touch event you mentioned does not get through to your button, and no click is performed.
You should move the button code into an OnDismissListener, if you want it to run whenever the window is dismissed.

Android: What's the difference between a boolean for a click and button.isPressed()?

There are a lot of options on how to define a click/tap on the touchscreen. One of them for example is setting a boolean.
Example for boolean:
boolean buttonClicked = true;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (buttonClicked) {
//do that and this
}
}
});
And there's a isPressed() method:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (button.isPressed()) {
//do that and this
}
}
});
What exactly is the difference between them? And when and why do I use boolean and the method isPressed()?
Because you are referring to a button in both of your examples, I assume that you are referring to the user tapping on a button, not just a random touch on the screen.
That being said, both of the examples you provided are not good.
In your first example, the boolean is useless because it is always true, so //do that and this will always be reached.
In your second example, your if statement is useless, because the onClick method by its nature is only reached when the button is tapped.
A good way to listen for a button press is using a click listener like this:
Button button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Code placed here will run every time the button is tapped
}
});
...where R.id.buttonId is the ID of your button in the layout.
If you need to define click event for a View you can use onClickListener, onTouchListener.
For more information check for Android official Documentation.
onTouchListener
onTouchListener
When considering your first code snippet, You can use boolean to perform another operation on button click event. as example something like this ,
boolean buttonClicked = false;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//true after button clicked
buttonClicked = true;
}
});
//if buttonClicked equals true
if (buttonClicked){
//perform operation only after button clicked
}
when considering your second code snippet, no need of button.isPressed() inside
button's onClick() callback. Because what you want to do by checking button.isPressed() is done without it inside button's onClick() callback.
Keep in mind these things.
isPressed() is a public method of View Class
Button is a subclass of View Class
isPressed() is a public method of Button Class as well.
About isPressed() from Android official documentation.
Indicates whether the view is currently in pressed state. Unless
setPressed(boolean) is explicitly called, only clickable views can
enter the pressed state.
Returns true if the view is currently pressed, false otherwise.

Categories

Resources