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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:20:05+00:00 2026-06-09T02:20:05+00:00

I am currently trying to keep an alert dialog with edit text showing on

  • 0

I am currently trying to keep an alert dialog with edit text showing on the screen. As usual there are Okay and Cancel buttons and I wanted to add a little error handling to the process. If the user does not enter any text in the edit text box and tries to press okay, I want a toast message to pop up notifying them of their error. The dialog box should still be kept showing because they still need to enter something if they want to press okay.

This is what I have so far. BTW, the dialog box pops up based on what the user selected from a drop down list. Any help in getting the dialog box to stay up would be greatly appreciated!!!

Here is my code:

    regionSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View v, int position, long rowId)
        {
            regionSelect = regionSpinner.getItemAtPosition(position).toString();
            regionInteger = (int) regionSpinner.getItemIdAtPosition(position);




            chosenRegion = regionSpinner.getSelectedItem().toString();

            if (chosenRegion.equals(ENTER_OPTION))

            {
                final AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this);

                alert.setTitle(TITLE_DIALOG);

                // Set an EditText view to get user input
                 final EditText input = new EditText(LoginActivity.this);
                alert.setView(input);

                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        if(input.getText().toString().length() > 0 && input != null)
                        {
                        textValueForEnteredRegion = input.getText().toString();
                        Toast.makeText(getApplicationContext(), "Input Accepted", Toast.LENGTH_SHORT).show();


                        dialog.dismiss();
                        //added soft keyboard code to make keyboard go away.
                        getWindow().setSoftInputMode(
                                  WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


                        }
                        else
                        {

                            Toast.makeText(getApplicationContext(), "You must enter a selection.", Toast.LENGTH_SHORT).show();

                        }
                    }


                });



                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        // Canceled.
                        dialog.dismiss();

                        //added soft keyboard code to make keyboard go away.
                        getWindow().setSoftInputMode(
                                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


                    }
                });

                alert.show();



            }


        }



        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {

        }


    });
  • 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-09T02:20:06+00:00Added an answer on June 9, 2026 at 2:20 am

    Override the button’s onClick methods.

    See: Android SDK override AlertDialog events

        AlertDialog.Builder build=new AlertDialog.Builder(this);
    build.setMessage("This is a message, dawg.")
    .setPositiveButton("Awesomesauce.", new OnClickListener()
    {
        public void onClick(DialogInterface dialog, int id)
        {
            //Change stuff in here so that dialog.dismiss() is not called.
            //dialog.dismiss(); //closes the window
        }
    };
    AlertDialog myAlert=build.create();
    myAlert.show();
    

    Edit: Looks like you already have this… you just need to get rid of dialog.dismiss() in your onPostitiveButton onClick method, and put a conditional around it that checks to see if your EditText has some text in it.

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

Sidebar

Related Questions

I'm currently struggling a bit trying to keep my controller specs DRY and succinct
I'm currently struggling to use UI elements in Interface Builder. I keep trying to
I'm currently trying emacs coming from vim. There is only one thing I desperately
I'm currently trying to optimize an database by combining queries. But I keep hitting
I'm currently writing up some cucumber specs, and I'm trying to keep features organised
I am currently trying to make a screen recorder. I made list of bitmap
I am trying to keep elements in header in one row. My current styles
Currently trying at add ajax to a site, after much reading I discovered that
Im currently trying to downlaod a audio track from a WCF, i need some
Im currently trying to learn some stuff about encryption, it's algorithms and how it

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.