How to restrict alert dialog close upon clicking search button of device? - android

I have to restrict alert dialog getting closed on clicking on search button of the device, how to achieve it in android?
Scenario:
My app has one activity with list of data, user can select the data in the list and perform edit, delete etc... on it, if user tries to press edit,delete button of App without selecting an item from list then it pops up alert dialog with message saying "Please select an item" with Ok button.So when the alert dialog pops up if user presses Search button of the device , alert dialog gets closed, I just want to restrict closing of alert dialog.
Please help me to resolve this.

Try this :
dialog.setCanceledOnTouchOutside(false);
It will restrict close only out-side click of alert dialog.

Set key Listener to your AlertDialog Builder and track the KeyEvent.KEYCODE_SEARCH. Or Override onKeyDown() in your Activity..
For AlertDialog Builder Something like,
.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
return true;
}
return false;
}
})

Use Dialog.setCancelable(false); before showing the dialog
Else you can handle the button press like how others have explained here

Related

How to dismiss Android Dialog when click outside (using FLAG_NOT_FOCUSABLE)

I have a dialog that I want to be dismissed when I click outside it. However, I don't want it to receive key events, because it's a volume dialog, and I want the activity to be able to receive the key volume up and down events. So I setted FLAG_NOT_FOCUSABLE, as you see below:
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.MyDialogTheme);
AlertDialog alert = builder.create();
Window window = alert.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
alert.setView(view);
the problem is that, with this flag, I also can't make the dialog dismiss when a touch outside it happens.
All the solutions on How to dismiss the dialog with click on outside of the dialog? like dialog.setCanceledOnTouchOutside(true); wont work in this case.
1 - Set the flag-FLAG_NOT_TOUCH_MODALfor 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;
}

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();
}
}

setCancelable(false) and onBackPressed conflict

I have a problem
I have a non cancelable custom dialog
which mean this custom dialog can only close if presses buton inside the custom dialog, so it won't cancel on backpress or click outside
I tried setCancelable(false) and it works however in my activity I have a onBackPressed and whenever my non cancelable dialog show onBackPressed wont trigger when I click back button because I think they conflict
is their a solution to do this?
EDIT: The purpose is I want the user to click button ok, or skip inside the custom dialog which means this dialog is required before proceeding to next activity
also in onBackPressed since I am using fragment whenever user press back it changes to previous fragment
sorry for lacking of explanation
my code is this
Dialog
dialog_welcome_navigation = DialogUtils.showCustomDialog(context, R.layout.dialog_welcome_navigation);
dialog_welcome_navigation.setCancelable(false); // disable closing dialog with back pressed
dialog_welcome_navigation.setCanceledOnTouchOutside(true);
and the onBackPressed
#Override
public void onBackPressed(){
Log.d("TAG", "--back--");
}
After searching I have found a solution thanks to this SO answer
https://stackoverflow.com/a/25251122/3481654
I add a setOnKeyListener on my dialog
dialog_welcome_navigation.setOnKeyListener(dialogWelcomeNavigationOnKey);
private DialogInterface.OnKeyListener dialogWelcomeNavigationOnKey = new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
dialog_welcome_navigation.dismiss();
// move other fragment
return true;
}
return false;
}
};

How do I change keyboard button "Go" functionality

My app begin with a users login.
when the user push the "Enter" button (after he mark himself on the gridview that gets data from sqlite). The app open alertdialog by inflater.
if (gridView.isClickable()){
Toast.makeText(getApplicationContext(), "Waiter selected", Toast.LENGTH_LONG).show();
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.password_dialog, null);
AlertDialog.Builder passwordDialog = new AlertDialog.Builder(MainActivity.this);
The XML file(layout) is only with an editext, on this editext the user needs to fill in
his own password:
passwordDialog.setTitle(getString(R.string.get_id_uniq));
passwordDialog.setMessage(getString(R.string.enter_id));
passwordDialog.setView(dialogLayout);
passwordDialog.setPositiveButton(getString(R.string.next),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
password = input.getText().toString();
and now the problem, when the user click on the editext the keyboard open and
then he puts his password, when he finish and press on the "Go" button on
the keyboard,the keyboard close but that not enough cause now he need to push again on the alertdialog button and only then the app will check if the password is correct and will move him to the next activity.
i tried to use -
android:imeOptions="actionNext"
and also the "Done", "Go" & "Send" , none of them help.
how do i prevent the double click on 2 different buttons and send the data from the keyboard button and skip the need to push the dialog button?
You can do it like this:
change dialog type to AlertDialog, instead of AlertDialog.Builder - that will allow you to dismiss (close) your dialog.
use setOnEditorActionListener for you input, instead of setPositiveButton
Call dialog.dismiss(); When user click done on keyboard.
It should look something like that:
AlertDialog dialog = new AlertDialog.Builder(this).create();//note dialog's type
dialog.setTitle("Let's check it");
EditText input = new EditText(this);
dialog.setView(input);//assume this is your input
input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE){
//What to do when user clicked Done button
Log.i("user's password:", v.getText().toString());//v.getText().toString(); is what's user has entered
dialog.dismiss();//close Alert Dialog
return true;
}
return false;
}
});

Display and Hide alternatively a dialog on touching anywhere on screen in android

I am developing a small app wherein a dialog must popup when the user touches anywhere on the screen and if the dialog is already being displayed then on clicking anywhere outside the dialog box it must disappear. Someone plz give suggestions as to how to go about doing this.
This is possibly duplicate of link
if you want to hide dialog box after touch event then
Dialog dialog = new Dialog(context);
dialog.setCanceledOnTouchOutside(true);
and by overriding the onTouch listener as
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
dialog.dismiss();
}
return false;
}

Categories

Resources