Search code examples
javaandroidandroid-alertdialog

Change the Text of AlertDialog positiveButton after clicking


I have an AlertDialog that has a positive button (OK) and a cancel. When pressing OK, I have a process that should take around 10s to finish (downloading an image from a URL and then showing it on the main page). How can I change the positiveButton text after pressing to be something like "loading"? And also, it should wait until the image is downloaded, so the alert shouldn't disappear.

I tried looking into its functions but didn't see a setText() function.

    myButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
        builder.setTitle("What's your question?");

        // Set up the input
        final EditText input = new EditText(v.getContext());
        // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);
        input.setText("test test");
        input.setTextSize(15);

        // Set up the buttons
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            m_text = input.getText().toString();
          }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
          }
        });

        builder.show();
      }
    });

Solution

  • Try this:

     myButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
            builder.setTitle("What's your question?");
        
            // Set up the input
            final EditText input = new EditText(v.getContext());
            // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
            input.setInputType(InputType.TYPE_CLASS_TEXT);
            builder.setView(input);
            input.setText("test test");
            input.setTextSize(15);
        
            // Set up the buttons
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(final DialogInterface dialog, int which) {
                // Disable the button to prevent further clicks
                final AlertDialog alertDialog = (AlertDialog) dialog;
                alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
                alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setText("Loading...");
        
                m_text = input.getText().toString();
        
                // Simulate a 10s delay for downloading the image (Replace this with your actual download code)
                new Handler().postDelayed(new Runnable() {
                  @Override
                  public void run() {
                    // After the download is complete, update the button text and enable it
                    alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setText("OK");
                    alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
        
                    // Add code here to show the downloaded image on the main page
                  }
                }, 10000); // Simulated 10s delay, replace with your actual download time
              }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
              }
            });
        
            builder.show();
          }
        });