Tutankhamen
Tutankhamen

Reputation: 3562

Add controls to custom dialog programmatically

I want to show a dialog with ~50 custom controls (switch buttons) on it. So, the best way to do that is to add them programmatically in a loop. I've tried to make a dilog with a layout wich contains the only one GroupView element:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="#AAAAAA"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ViewGroup
        android:layout_height="500dp"
        android:layout_width="500dp"
        android:id="@+id/dlg_view"/>
</LinearLayout>

and then just add my controls in it using: onCreateDialog(...) method:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.geomap_menu, null))
          .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int id) {
                 // sign in the user ...
              }
          })
          .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                  //LoginDialogFragment.this.getDialog().cancel();
              }
});

Dialog res = builder.create();
ViewGroup dlgView = (ViewGroup)res.findViewById(R.id.dlg_view);
MyControl myControl = new MyControl(this);
dlgView.add(myControl);

But it doesn't work this way (it throws InflateException). What am I doing wrong?

Upvotes: 1

Views: 2993

Answers (1)

user
user

Reputation: 87064

The problems in your code were pretty obvious:

  • In your layout file you use ViewGroup which is an abstract class(the root of all layouts in Android) and which can't be instantiated so it will most likely be the reason for that inflate exception you talk about. Use one of the subclasses of ViewGroup, like LinearLayout, RelativeLayout etc, which one fits you better.

  • Even after doing the modification I wrote above your code will still bot work. First the ViewGroup class doesn't have an add method, you're probably referring to one of the addView methods. Second the dlgView will be null because at that moment the Dialog isn't displayed so there is no View to find. You can do it by posting a Runnable on one of your views to delay setting the views until the Dialog is shown:

    final Dialog res = builder.create();
    oneOfYourViews.post(new Runnable() {
    
        @Override
        public void run() {
            ViewGroup dlgView = (ViewGroup) res.findViewById(R.id.dlg_view);
            MyControl myControl = new MyControl(context);
            dlgView.addView(myControl);             
        }
    });
    

Code addition:

View contentView = inflater.inflate(R.layout.geomap_menu, null)
ViewGroup dlgView = (ViewGroup) contentView.findViewById(R.id.dlg_view);
MyControl myControl = new MyControl(this);
dlgView.addView(myControl); // or add the other views in the loop as many as you want
builder.setView(contentView);
// rest of your code

Upvotes: 1

Related Questions