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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:39:52+00:00 2026-06-09T05:39:52+00:00

I want to create a dialogBuilder with a text field and a button on

  • 0

I want to create a dialogBuilder with a text field and a button on it. The idea is to make the program wait for any further actions until the text in the field is entered and the OK button is clicked. Below is the code:

private static final Object wait = new int[0];
private static String result = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Handler h = new Handler();
    final Context context = MainActivity.this;
    h.post(new Runnable() {

        public void run() {
            final Builder dialogBuilder = new AlertDialog.Builder(context);
            dialogBuilder.setTitle(R.string.app_name);
            final LinearLayout panel = new LinearLayout(context);
            panel.setOrientation(LinearLayout.VERTICAL);
            final TextView label = new TextView(context);
            label.setId(1);
            label.setText(R.string.app_name);
            panel.addView(label);

            final EditText input = new EditText(context);
            input.setId(2);
            input.setSingleLine();
            input.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_URI
                    | InputType.TYPE_TEXT_VARIATION_PHONETIC);
            final ScrollView view = new ScrollView(context);
            panel.addView(input);
            view.addView(panel);

            dialogBuilder
                    .setCancelable(true)
                    .setPositiveButton(R.string.app_name,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    result = input.getText().toString();

                                    synchronized (wait) {
                                        wait.notifyAll();
                                    }

                                    dialog.dismiss();
                                }
                            }).setView(view);

            dialogBuilder.setOnCancelListener(new OnCancelListener() {
                public void onCancel(DialogInterface arg0) {
                    result = null;
                    synchronized (wait) {
                        wait.notifyAll();
                    }
                }
            });
            dialogBuilder.create().show();
        }

    });

    String localResult = null;
    try {
        synchronized (wait) {
            Log.d("Waiting", "Waiting " + localResult);
            wait.wait();
        }
        localResult = result;
        result = null;
        if (localResult == null) {
            // user is requesting cancel
            throw new RuntimeException("Cancelled by user");
        }
        Log.d("RESULT ", "RESULT " + localResult);
    } catch (InterruptedException e) {
        localResult = result;
        result = null;

        if (localResult == null) {
            // user is requesting cancel
            Log.d("CANCELED ", "CANCELED " + localResult);
            throw new RuntimeException("Cancelled by user");
        }
    }
    Log.d("RESULT AFTER THE DIALOG", "RESULT AFTER THE DIALOG " + result);
}

The program is going to Log.d(“Waiting”, “Waiting ” + localResult); and after that just waiting. NO DIALOG BUILDER IS SHOWN on the activity window. I used the debug mode and saw that the program flow is not entering the run() method, but the value of the Handler.post() is true. And for this reason the dialog is not shown, and the program is waiting.

I have tried to remove the moment with waiting (remove the Handler.post()), just to see if the dialog will show, and it showed and all moved well, but the result was not I am needing – I want the program to wait the input from the dialog … I am really out of ideas.

Would you please give me some suggestions as I am really out of ideas.

Thanks a lot!

  • 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-09T05:39:54+00:00Added an answer on June 9, 2026 at 5:39 am

    You should be running the display of the Dialog in the UI Thread, not a seperate thread.

    An example would be something like this:

    In the onCreate()

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // Display progress dialog when loading contacts
                                dialog = new ProgressDialog(this);
                                // continue with config of Dialog
                            }
                        });
    
                            // Execute the Asynchronus Task
                            new AsyncTask<Void, Void, Void>() {
                                @Override
                                protected Void doInBackground(Void... params) {
                                    // code to execute in background
                                    return null;
                                }
                                @Override
                                protected void onPostExecute(Void result) {
                                    // Dismiss the dialog after inBackground is done
                                    if (dialog != null)
                                        dialog.dismiss();
    
                                    super.onPostExecute(result);
                                }
    
                            }.execute((Void[]) null);
    

    Specifically what is happening here is the Dialog is being displayed on the UI thread and then the AsyncTask is executing in the background while the Dialog is running. Then at the end of the execution we dismiss the dialog.

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

Sidebar

Related Questions

I want create an application with animate button? how can i do? after click
I want to create a new field (or two) in my table that is
I want create some view, and add dynamically to it text and images Somebody
I want create texture atlas(matrix of images) from multiple images.Is their any applescript that
I want create a button option that takes the entire datalist and converts it
I want create a query that creates a calculated field ANSWER_SCORE which it then
i want create one simple windows form ,it just contain Editcontrol(textbox),Static Edit(label),Button (Name of
I want create a UIImageView with a button press. I did this with: -
i want create multiple search where statement $where_search is a multiple condition from post
I want create wordpress website into which I want create user management... That means

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.