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

The Archive Base Latest Questions

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

We can perform a validation by adding a positive button with implementation final EditText

  • 0

We can perform a validation by adding a positive button with implementation

final EditText urlEditText = new EditText(this);

DialogInterface.OnClickListener event = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                try
                {
                    // Validate URL
                    new URL(urlEditText.getText().toString());
                    Toast.makeText(getBaseContext(), "URL Accepted", 1).show();
                }
                catch (MalformedURLException e) {
                    Toast.makeText(getBaseContext(), "Invalid URL", 1).show();
                }
            }
        };

AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("Enter the server URL here")
                    .setNegativeButton("Cancel", null)
                    .setPositiveButton("Ok",event)
                    .setView(urlEditText)
                    .create();
        dialog.show();

If we enter a wrong URL it will be validated and display a wrong URL Toast. However, the dialog is dismissed it should be still displayed until the user press “Cancel” or enter the correct URL. There are solutions around here like recreating the dialog and display it to user again but this would be a good idea.

Is there a way that If we enter a wrong URL the AlertDialog still there?

  • 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:17:49+00:00Added an answer on June 18, 2026 at 3:17 pm

    We can use AlertDialog.getButton() so we can get button reference of AlertDialog. This should be called if the dialog is shown to user.

    final EditText urlEditText = new EditText(this);
    
    
    final AlertDialog dialog = new AlertDialog.Builder(this)
                        .setTitle("Enter the server URL here")
                        .setNegativeButton("Cancel", null)
                        .setPositiveButton("Ok",null)
                        .setView(urlEditText)
                        .create();
            dialog.setOnShowListener(new OnShowListener() {
    
                @Override
                public void onShow(DialogInterface arg0) {
    
                    Button okButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
                    okButton.setOnClickListener(new View.OnClickListener() {
    
                        @Override
                        public void onClick(View arg0) {
    
                            try
                            {
                                new URL(urlEditText.getText().toString());
                                Toast.makeText(getBaseContext(), "URL Accepted", 1).show();
                                dialog.dismiss();
                            }
                            catch (MalformedURLException e) {
                                Toast.makeText(getBaseContext(), "Invalid URL", 1).show();
                            }
                        }
                    });
                }
            });
    

    UPDATE 1: We can use AlertDialog.BUTTON1 but it is now deprecated.

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

Sidebar

Related Questions

How can we perform Email Validation on edittext in android ? I have gone
How can I perform a simple validation on this HTA form to make sure
How can I require the Address element? If I perform an XSD validation on
It seems that org.hibernate.cfg.Configuration object can be used to perform validation programmatically, by calling
How can I perform a text validation on a textbox in form written in
we can perform vim substitute on a set of lines by selecting them in
can anyone suggest me a plugin/tool that can perform a code formatting at build
Let's assume that our system can perform actions, and that an action requires some
Using the JavaScript Native Interface of GWT I can perform the following: public native
Let's say you have a business logic method that can perform some operation across

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.