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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:43:15+00:00 2026-05-27T01:43:15+00:00

Please look at the custom dialog below. I have an edittext field on the

  • 0

Please look at the custom dialog below. I have an edittext field on the dialog and if the text field is empty I would like to disable the positiveButton. I can get a charListener for the text field but I am not sure how I am going to set the positivebutton to disable or enable from that listener? What is the reference for the positive and negative buttons?

 case DIALOG_TEXT_ENTRY:
    // This example shows how to add a custom layout to an AlertDialog
    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
    return new AlertDialog.Builder(AlertDialogSamples.this)
        .setIconAttribute(android.R.attr.alertDialogIcon)
        .setTitle(R.string.alert_dialog_text_entry)
        .setView(textEntryView)
        .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked OK so do some stuff */
            }
        })
        .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked cancel so do some stuff */
            }
        })
        .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-27T01:43:16+00:00Added an answer on May 27, 2026 at 1:43 am

    Edit for complete solution…

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setIcon(android.R.drawable.ic_dialog_info);
    builder.setTitle("Alert dialog title");
    builder.setMessage("This is the example code snippet to disable button if edittext attached to dialog is empty.");
    builder.setPositiveButton("PositiveButton",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    // DO TASK
                }
            });
    builder.setNegativeButton("NegativeButton",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    // DO TASK
                }
            });
    
    // Set `EditText` to `dialog`. You can add `EditText` from `xml` too.
    final EditText input = new EditText(MainActivity.this);
    
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
    );
    input.setLayoutParams(lp);
    
    
    builder.setView(input);
    
    final AlertDialog dialog = builder.create();
    dialog.show();
    
    // Initially disable the button
    ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    
    // OR you can use here setOnShowListener to disable button at first time.
    
    // Now set the textchange listener for edittext
    input.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }
    
        @Override
        public void afterTextChanged(Editable s) {
    
            // Check if edittext is empty
            if (TextUtils.isEmpty(s)) {
                // Disable ok button
                ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    
            } else {
                // Something into edit text. Enable the button.
                ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
            }
    
        }
    });
    

    Below are edited history, which can be refer as some more details

    Here is a sample code, try this

    AlertDialog.Builder builder = new AlertDialog.Builder(AddSchedule.this);
    builder.setIcon(android.R.drawable.ic_dialog_info);
    builder.setTitle("Alert dialog title");
    builder.setMessage("Dialog message");
    builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            //DO TASK
        }
    });
    builder.setNegativeButton("Button2", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            //DO TASK
        }
    });
    
    AlertDialog dialog = builder.create();
    dialog.show();
    
    // After calling show method, you need to check your condition and enable/disable the dialog buttons 
    if (your_condition_true) {
        // BUTTON1 is the positive button
        dialog.getButton(AlertDialog.BUTTON1).setEnabled(false);
    }
    

    For negative button

    dialog.getButton(AlertDialog.BUTTON2).setEnabled(false); //BUTTON2 is negative button
    

    For buttons id : Reference alert_dialog.xml

    Edited :

    And the setOnShowListener since level 8 API (FroYo), does the same,

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setPositiveButton(android.R.string.ok, null);
    
    AlertDialog dialog = builder.create();
    dialog.setOnShowListener(new OnShowListener() {
    
        @Override
        public void onShow(DialogInterface dialog) {
            if (condition) {
                ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
            }
        }
    });
    
    dialog.show();
    

    Edited

    new AlertDialog.Builder(this)
        .setMessage("This may take a while")
        .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);
                // the rest of your stuff
            }
    
        }).show();
    

    • 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 tab layout which look like this,I want to make
I would like to create a custom look for a tooltip. <h:outputText value=blarg title=tooltip>
Please look at the two examples below: irb(#<ActionView::Base:0x2863d58>):030:0> paintings_path => /some-nice-alias-path irb(#<ActionView::Base:0x2863d58>):029:0> self.controller_name.to_s +
Please look at : http://news.bbc.co.uk/2/hi/europe/8592380.stm The content updates automatically, however, I can't see it
Please look at the below code snippet and let me know how the out
First, please look at this custom Button-inherited UserControl code: Public Class UserControl1 Dim _Text
Please have a look on this application http://itunes.apple.com/app/yowza-mobile-coupons/id312021877?mt=8 I just want to add custom
I want my activity to look like a dialog. Well, I achieved that using
Please look at this image here is 3 tables , and out i want
Please look that alias name. I hope to set the value into a string

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.