How to 'cancel' a button press?

The Clueless Programmer

I am currently trying to work with AlertDialogs.

Currently, I have an EditText in my AlertDialog, and want to restrict the input to only Integers. I have used a basic try and catch block to avoid the app crashing form a NumberFormatException.

However, I want to set it up so that when the user tries to press the button with the incorrect input, the input does not register and the Dialog is not cancelled.

UpperLimitDialog.setPositiveButton(R.string.Positive_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        int RawInput = settings.getInt("UpperLimit", 12);
                        try {
                            RawInput = Integer.parseInt(input.getText().toString());
                        }catch (NumberFormatException se) {
                            Toast.makeText(SettingsMenu.this, "Non-Integer Value, No Input", Toast.LENGTH_SHORT).show();
                        //Here I want the app to not register the click and prevent the dialog box from closing.
                        }
                        editor.putInt("UpperLimit", RawInput);
                        editor.apply();
                        Toast.makeText(SettingsMenu.this, "Set Upper Limit to " + RawInput, Toast.LENGTH_SHORT).show();
                    }
                });

What method can I use to achieve this?

Lino

The basic trick here is to use the dialog's setCancelable to false and call dismiss() only when the input has been validated.

More info here: AlertDialog with positive button and validating custom EditText

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift, how to play sound when press a button

From Dev

How to access HTML of webpage after button press

From Dev

Kivy: How to call something on button on_press

From Dev

How to press the back button programmatically in android

From Dev

How to handle Back Button press in android fragments

From Dev

Cancel button press if user moves finger off button

From Dev

How to cancel QProcess by clicking in QProgressDialog's cancel button?

From Dev

How can my program randomly press a button?

From Dev

How to change Tkinter label text on button press

From Dev

How to change background color of button on press in kivy?

From Dev

How to make press and hold button to Start a timer?

From Dev

how to cancel a localNotification with the press of a button in swift?

From Dev

How to cancel a download request when cancel button pressed in swift

From Dev

How to programmatically press QML Button?

From Dev

Return textbox value after press cancel button

From Dev

How to draw a circle on button press

From Dev

How to press a login button of a website programmatically

From Dev

How to programatically simulate a button press?

From Dev

How can I tell Selenium to press cancel on a print popup?

From Dev

How to detect SearchView's back button press?

From Dev

Rails - How To Transfer Data With Button Press

From Dev

Second press required on cancel button

From Dev

Cancel button press if user moves finger off button

From Dev

onActivityResult error when I press "cancel" button on my second Activity

From Dev

Return textbox value after press cancel button

From Dev

how to cancel the asynchronous task on back press in activity

From Dev

How to 'cancel' a button press?

From Dev

How to exit MsgBox when user press's cancel

From Dev

How to change Image of button when press and Back press?

Related Related

  1. 1

    Swift, how to play sound when press a button

  2. 2

    How to access HTML of webpage after button press

  3. 3

    Kivy: How to call something on button on_press

  4. 4

    How to press the back button programmatically in android

  5. 5

    How to handle Back Button press in android fragments

  6. 6

    Cancel button press if user moves finger off button

  7. 7

    How to cancel QProcess by clicking in QProgressDialog's cancel button?

  8. 8

    How can my program randomly press a button?

  9. 9

    How to change Tkinter label text on button press

  10. 10

    How to change background color of button on press in kivy?

  11. 11

    How to make press and hold button to Start a timer?

  12. 12

    how to cancel a localNotification with the press of a button in swift?

  13. 13

    How to cancel a download request when cancel button pressed in swift

  14. 14

    How to programmatically press QML Button?

  15. 15

    Return textbox value after press cancel button

  16. 16

    How to draw a circle on button press

  17. 17

    How to press a login button of a website programmatically

  18. 18

    How to programatically simulate a button press?

  19. 19

    How can I tell Selenium to press cancel on a print popup?

  20. 20

    How to detect SearchView's back button press?

  21. 21

    Rails - How To Transfer Data With Button Press

  22. 22

    Second press required on cancel button

  23. 23

    Cancel button press if user moves finger off button

  24. 24

    onActivityResult error when I press "cancel" button on my second Activity

  25. 25

    Return textbox value after press cancel button

  26. 26

    how to cancel the asynchronous task on back press in activity

  27. 27

    How to 'cancel' a button press?

  28. 28

    How to exit MsgBox when user press's cancel

  29. 29

    How to change Image of button when press and Back press?

HotTag

Archive