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

  • Home
  • SEARCH
  • 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 9269337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:05:42+00:00 2026-06-18T15:05:42+00:00

I have an AlertDialog with a custom layout (just an EditText) and I want

  • 0

I have an AlertDialog with a custom layout (just an EditText) and I want to validate the data when the OK button is clicked. If the validation fails I don’t want to close the dialog.

I’m using dialog’s default buttons (positive and negative). If I use “setPositiveButton(“”, new DialogInterface.OnClickListener() …” the dialog is always closed. I’ve seen several posts and they said that the onClick Listener should be override, but I can’t get it working. This is the code I found:

Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));

Since it says it should be done AFTER showing the dialog I placed this code inside my activity, not inside my DialogFragment, but if I use mDialogFragment.getDialog() it always returns null.

This is a part of my Dialog Fragment:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(R.string.new);

    LayoutInflater inflater = getActivity().getLayoutInflater();
    dialogView = inflater.inflate(R.layout.edit_license, null);

    builder.setView(dialogView)

    // Add action buttons
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

        }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            MyDialogFragment.this.getDialog().cancel();
        }
    }); 

    return builder.create();

}

And in my Activity I do the following:

DialogFragment dialog = new MyDialogFragment(true, null);
dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment");

AlertDialog alertDialog = (AlertDialog)dialog.getDialog();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
btnPositive.setOnClickListener(new CustomListener(alertDialog));

... 

class CustomListener implements View.OnClickListener {
    private final Dialog dialog;
    public CustomListener(Dialog dialog) {
        this.dialog = dialog;
    }
    @Override
    public void onClick(View v) {
        ...
    }
}

Like I said, (AlertDialog)dialog.getDialog(); always returns null. Why is that? How can I avoid closing the dialog if the validation is not ok?

Thanks!

  • 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-06-18T15:05:44+00:00Added an answer on June 18, 2026 at 3:05 pm

    here is some text from reference of DialogFragment:

    A fragment that displays a dialog window, floating on top of its
    activity’s window. This fragment contains a Dialog object, which it
    displays as appropriate based on the fragment’s state. Control of the
    dialog
    (deciding when to show, hide, dismiss it) should be done
    through the API here, not with direct calls on the dialog.

    Instead of calling getDialog().dismiss() you should do dismiss()

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

Sidebar

Related Questions

I have created a custom AlertDialog with multiple EditText fields using a custom layout.
I have a Custom AlertDialog.Builder but I couldn't reach the EditText. This is my
I have a custom alertdialog that inflates this custom layout: <?xml version=1.0 encoding=utf-8?> <RelativeLayout
I have a custom dialog with an EditText and Button in it. In my
I have a AlertDialog that uses a Custom Layout. In the OnClick Event I
I have an AlertDialog in my application. It contains a list of custom views
I have created an AlertDialog with OK button. But it is only displaying the
I have button that a user selects and I pop up a AlertDialog to
I have been working with custom AlertDialog selections for the last few days and
I have subclassed SimpleCursorAdapter in order to create a custom list entry layout containing

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.