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 6550157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:10:56+00:00 2026-05-25T12:10:56+00:00

I am using a simple text field alert dialog with a positive and a

  • 0

I am using a simple text field alert dialog with a positive and a cancel button. I want to validate my alert dialog and prevent the done button from closing the AlertDialog if the input is invalid.

Is there any way short of creating a custom dialog to prevent the PositiveButton onClick() handler from closing the dialog if the validation fails?

class CreateNewCategoryAlertDialog {
    final EditText editText;
    final AlertDialog alertDialog;

    class PositiveButtonClickListener implements OnClickListener {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String name = editText.getText().toString();
            if(name.equals("")) {
                editText.requestFocus();
                editText.setError("Please enter a name");
                // Some code to stop AlertDialog from closing goes here...
            } else {
                doSomethingUsefulWithName();
            }
        }
    }

    AlertDialog buildAlertDialog(Context context) {
        return new AlertDialog.Builder(context)
        .setTitle(context.getString(R.string.enter_name))
        .setMessage(context.getString(R.string.enter_name_msg))
        .setView(editText)
        .setPositiveButton(context.getString(R.string.done), new PositiveButtonClickListener())
        .setNegativeButton(context.getString(R.string.cancel), null).create();
    }
}
  • 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-25T12:10:56+00:00Added an answer on May 25, 2026 at 12:10 pm

    Here’s how I did it. Technically, it doesn’t technically keep the dialog open, it closes it momentarily and re-opens it, but the net result is the same.

    class MyAlertDialog implements OnDismissListener, OnCancelListener {
        final private EditText editText;
        final private AlertDialog alertDialog;
        final private EventManager eventManager;
        final private CategorySelector categorySelector;
    
        private Boolean canceled;
    
        MyAlertDialog(Context context) {
            editText = new EditText(context);
            alertDialog = buildAlertDialog(context);
            alertDialog.setOnDismissListener(this);
            alertDialog.setOnCancelListener(this);
            show();
        }
    
        private AlertDialog buildAlertDialog(Context context) {
            return new AlertDialog.Builder(context)
            .setTitle(context.getString(R.string.enter_name))
            .setMessage(context.getString(R.string.enter_name))
            .setView(editText)
            .setNeutralButton(context.getString(R.string.save_text), null)
            .setNegativeButton(context.getString(R.string.cancel_text), null)
                .create();
        }
    
        public void show() {
            canceled = false;
            alertDialog.show();
        }
    
        @Override public void onDismiss(DialogInterface dialog) {
            if(!canceled) {
                final String name = editText.getText().toString();
                if(name.equals("")) {
                    editText.setError("Please enter a non-empty name");
                    show();
                } else {
                    doWhateverYouWantHere(name);
                }
            }
        }
    
        @Override public void onCancel(DialogInterface dialog) {
            canceled = true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form that I want to validate using blur if the
I'm using Filemaker Pro's Instant Web Publishing feature. I've added a simple text field
This is simple text detection video made using an opencv. Any ideas how was
I'm programming a simple text-based RPG using a switch statement for a game loop.
Using JQuery, is there a simple way to select the text immediately after a
I have a simple text field on my page, with the initial text value
I'm using TinyMCE to provide users the capability of simple text formatting (bold, italics,
I have a very simple voting form. It contains a single text field for
I'm using a simple mysql LIKE query like this: SELECT * FROM myTable WHERE
I am looking for validation using jquery on a simple form and I want

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.