Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6202491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:44:42+00:00 2026-05-24T04:44:42+00:00

I’ve got a DialogPreference (more precisely an EditTextPreference) and want to perform some checks

  • 0

I’ve got a DialogPreference (more precisely an EditTextPreference) and want to perform some checks on the value the user has input. These checks shall be made when the user clicks ok, not already while he is typing. If everything is ok, the dialog shall close. If there is an error, an AlertDialog shall appear with an explanation what is wrong and an ok-button. This AlertDialog shall come into view “on top of” the DialogPreference’s dialog, and when the ok-button is clicked, the first dialog shall come into view again.

I tried to extend EditTextPreference and override the onClick(DialogInterface dialog, int which) method to do this:

@Override
public void onClick(DialogInterface dialog, int which) {
    boolean invalidData = false;
    // check input
    if (true) {
        invalidData = true;
    }
    if (which == DialogInterface.BUTTON_POSITIVE && invalidData) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage("Some message.")
        .setCancelable(false)
        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        }).create().show();
        //super.showDialog(new Bundle());
    } else {
        super.onClick(dialog, which);
    }
}

But this does not have the desired effect:

  • With the code above, the AlertDialog is shown and the value is not
    saved when the EditTextPreference’s positive button is clicked, but
    the EditTextPreference’s dialog is immediately closed.
  • If super.showDialog(new Bundle()); is uncommented, the AlertDialog
    is shown and above it immediately also the EditTextPreference’s
    dialog pops up again.

So how can I reach the desired behaviour?

EDIT: As according to hackbod this is not possible, I will use a solution that gets close. This is far from a good user experience, but as my app will be used by less than 100 people and I develop this in my spare time, I don’t want to put too much effort in it – like creating my own DialogPreference. This is what I use now:

public abstract class EditTextPreferenceWithCheck extends EditTextPreference {

    private boolean mAlertDialogActive;
    private String mCachedValue;
    private String mMessage = "";

    public EditTextPreferenceWithCheck(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public EditTextPreferenceWithCheck(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextPreferenceWithCheck(Context context) {
        super(context);
    }

    /**
     * Sets the message that will be shown if inputIsValid(String input) returns
     * false.
     * 
     * @param message The message to show
     */
    protected void setMessage(String message) {
        this.mMessage = message;
    }

    /**
     * Checks if the user input is valid. If not, the message set with
     * setMessage(String message) will be shown.
     * 
     * @param input Current value in the text field
     * @return true if the current value in the text field is valid, otherwise
     *         false
     */
    protected abstract boolean inputIsValid(String input);

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (mAlertDialogActive) {
            mAlertDialogActive = false;
            showDialog(new Bundle());
            getEditText().setText(mCachedValue);
        } else {
            if (which == DialogInterface.BUTTON_POSITIVE
                    && !inputIsValid(getEditText().getText().toString())) {
                mAlertDialogActive = true;
                mCachedValue = getEditText().getText().toString();
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setMessage(mMessage)
                        .setCancelable(false)
                        .setPositiveButton(android.R.string.ok, this).show();
            } else {
                super.onClick(dialog, which);
            }
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:44:42+00:00Added an answer on May 24, 2026 at 4:44 am

    Sorry, you can’t do this, EditTextPreference automatically dismisses its dialog when delivering a result and you can’t stop it from doing that.

    I suggest making your own DialogPreference that displays a custom dialog of your own when tapped. You can do the verification of the text inside that dialog since it is your own dialog. This will also give you a better user experience.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.