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?