eoinzy
eoinzy

Reputation: 2242

Android - dynamically adding view to popup dialog

I have an Alert Dialog that shows some text input fields. When I enter data and click "OK", it saves this data and displays it in a custom view on the Activity that called the dialog.

If I want to edit this data, I click on the custom view and the same alert dialog pops up. I want to pre-populate the data, and allow editing, and adding new data. However, here is where I'm having a problem.

When I try to do add the existing data to the dialog, the views get added to the top of the view, and not the subview I want them added to. See here:

Populated View show be under the headings

So the alert dialog inflates the view from my Activity:

View dialogView = LayoutInflater.from(this).inflate(R.layout.material_add_dialog, viewGroup, false);
builder.setView(dialogView);

But this R.layout.material_add_dialog is the main/top view.

I have a custom adapter class that I am using as well. Here is the getView() method:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = Objects.requireNonNull(mInflater).inflate(R.layout.add_material_popup_dialog_row, null);
    }

    Material material = mMaterials.get(position);

    EditText etMaterial = convertView.findViewById(R.id.material_add_text_material);
    EditText etSupplier = convertView.findViewById(R.id.material_add_text_supplier);
    EditText etWeight = convertView.findViewById(R.id.material_add_text_weight);
    EditText etAmount = convertView.findViewById(R.id.material_add_text_amount);

    etMaterial.setText(material.getMaterial());
    etSupplier.setText(material.getSupplier());
    etWeight.setText(material.getUnit());
    etAmount.setText(material.getAmount());

    return convertView;
}

So if you see convertView, this is inflating R.layout.add_material_popup_dialog_row which is the row that is being added at the top.

So really my question is, how can I add this row to a subview of the dialogs view?

Thanks.

Upvotes: 1

Views: 688

Answers (2)

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

You just need to get, or create, the subview. If it exists:

View dialogView = LayoutInflater.from(this).inflate(R.layout.material_add_dialog, viewGroup, false);
View subView = dialogView.findViewById(R.id.mySubViewId);

Or, you can create a new view and add it to the current view:

//You would need to create the appropriate view for your use-case.
View view = new View();
dialogView.addView(view);

Upvotes: 1

indra lesmana
indra lesmana

Reputation: 261

you can try to create the container LinearLayout as root then addView(convertView) and return root

Upvotes: 0

Related Questions