1

I am trying to show my custom view in a dialog bu using alertDialog.Builder with below code snippet.

AlertDialog.Builder alertViewBuilder = null;
AlertDialog alertViewDialog = null;
public void showPopupView(View popupView) {

if (alertViewBuilder == null) {
    alertViewBuilder = new AlertDialog.Builder(ctContext);
}
    alertViewBuilder.setView(popupView);

if (alertViewDialog == null)
    alertViewDialog = alertViewBuilder.create();

if (!alertViewDialog.isShowing())
    alertViewDialog.show();
}

if user perform any action on the components of that custom view, i wants to change/update the content of the dialog. so when user touches/clicks on any component of the custom view i am updating the popupView and passing the updated popupView to showPopupView(View popupView) again. but view is not getting updated on the dialog.

My requirement is similar the following example.

  1. showing login page with username, password Text-input Fields and "OK" button on a dialog.

  2. if user entered wrong username/password and press "OK" button the same Dialog should be updated with a view which will have an Error message textview instead of username & password Text-input Fields and "OK" button should be replaced with "Try Again" Button on the same Dialog.

  3. when user press "Try Again" button previous login page should be displayed on the same Dialog.

I mean flow should some thing similar to the changing view's on same activity. I think i've provided the sufficient information about my problem. Please suggest me some solution to my problem. Thanks in Advance.

1 Answer 1

-1

I am able to manage and find the solution for this issue. just removed AlertDialog.Builder and changed AlertDialog to Dialog and calling addContentView(view) in Dialog instead of calling setView(view) on AlertDialog.Builder. and adding and removing chaildviews on the contentview on the Dialog. find the below code snippet.

private android.view.ViewGroup.LayoutParams alertLayoutParams = null;
private Dialog alertViewDialog = null;
private LinearLayout cvPopupView;

public void showPopupView(View popupView) {
    if (cvPopupView == null)
        cvPopupView = new LinearLayout(ctContext);
    else
        cvPopupView.removeAllViews();

    cvPopupView.addView(popupView);

    if (alertViewDialog == null) {
        alertViewDialog = new Dialog(ctContext);
        alertViewDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    if (alertLayoutParams == null) {
        alertLayoutParams = new android.view.ViewGroup.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    if (!alertViewDialog.isShowing()) {
        alertViewDialog.addContentView(cvPopupView, alertLayoutParams);
        alertViewDialog.show();
    }
}

this may help, if any one else facing same kind of problem.

1
  • 1
    Can you please explain what does "addContentView" do? APIs say it just adds it, but that it can't be removed, and I also can't get those views. What if I want, for example, switch from full screen progress bar to a normal alertDialog style and vice versa? Mar 26, 2015 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.