1

I am trying to show an alert dialog with a progress bar and a message showing that "Bluetooth is connecting". I want to change that text to "Bluetooth Connected" when the connection is successful.

However, after calling the second time the alert dialog, I get an error

The specified child already has a parent. You must call removeView() on the child's parent first.

I've found several similar problems but couldn't manage to find a solution yet.

My Code:

I first call

showProgressBar("Connecting to Bluetooth...");

Then, based on the result of the callback method, I try to show again / update the alert dialog with different message

  @Override
/*
* Callback method that is triggered once we have a result about the bluetooth connection
* */
public void isBluetoothConnected(boolean isConnected)
{
    if (isConnected)
    {
        showProgressBar("CONNECTED!");
    }
    else
    {
        showProgressBar("Bluetooth Not Connected");
    }
}

And this is how I create the alert dialog

public void showProgressBar(String loadingStatus)
{
    activity.runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            if (progressDialog!=null)
            {
                progressDialog.dismiss();
            }

            progressDialog = getDialogProgressBar(loadingStatus).create();
            progressDialog.show();
        }
    });
}

public AlertDialog.Builder getDialogProgressBar(String loadingStatus)
{
    ProgressBar progressBar = new ProgressBar(context);
    if (builder == null) 
    {
        builder = new AlertDialog.Builder(context);
        builder.setCancelable(false);
        builder.setMessage(loadingStatus);
        builder.setNegativeButton("Cancel", null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        progressBar.setLayoutParams(lp);
        builder.setView(progressBar);
    }
    return builder;
}

My question is, while the alert dialog is shown, how can I update the displayed message every time?

John
  • 410
  • 1
  • 9
  • 21

2 Answers2

0

You would need to create an XML layout file containing a TextView and your progressbar instead of instantiating it in your code.

Apply your layout using dialog.setView(R.layout.yourLayout).

Then you can use dialog.findViewById(R.id.yourTextViewID) to get the desired TextView. Once you retrieved it, you can call setText(yourText) on the view.

You don't need to call dialog.setMessage(yourMessage) if you choose to go that way.

I hope this helps.

If you still hesitate, you can read this:

https://developer.android.com/guide/topics/ui/declaring-layout

Alternatively, you can setMessage on the dialog itself. (instead of builder)

https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/16105920/refresh-or-change-the-alertdialog-message&ved=2ahUKEwjez_bI9tnoAhWtzoUKHbWNBbUQjjgwAHoECAUQAQ&usg=AOvVaw1SuAepw8YnMEtSb4Ga2yeI

Samuel
  • 39
  • 1
  • 4
  • But even with this method, I would need to change the textview after the alert dialog is shown. However, once calling the alertDialog.show() I am not able to change neither the textview nor the message. – John Apr 08 '20 at 23:00
  • Of course you can edit the TextView when the dialog is showing.... – Samuel Apr 08 '20 at 23:01
  • Did you try it with a TextView? – Samuel Apr 08 '20 at 23:02
  • You simply create that layout and give the TextView an ID, then you update the TextView's content using its ID. It should work like a charm. – Samuel Apr 08 '20 at 23:05
  • I still get the error I mentioned on original post. I believe the problem is that I recreate the alert dialog every time I call the "showProgressBar()" method. But I don't know how I can correct it. – John Apr 08 '20 at 23:09
  • You should indeed call your method only once to create the dialog. Then when you catch your BT connected event, do as I described to update the TextView and don't create a new dialog. You can finally dismiss it when all your actions related to this dialog are over. – Samuel Apr 08 '20 at 23:12
  • Instead of creating the dialog in your if (isConnected), update TextView there. – Samuel Apr 08 '20 at 23:14
0

Try this

 @Override
/*
 * Callback method that is triggered once we have a result about the bluetooth connection
 * */
public void isBluetoothConnected(boolean isConnected)
{
    if (isConnected)
    {
        showProgressBar("CONNECTED!");
    }
    else
    {
        if(progressDialog != null && progressDialog.isShowing()) {
            progressDialog.setMessage("Bluetooth Not Connected");
        } else {
            // handle
        }
    }
}
vinv
  • 355
  • 2
  • 14