0

I'd like to create custom nested views like below:

It's easier to create a static custom view where all the components are defined in the view.

In this present scenario, I'd like to create custom view that inflates other views, but will also be a ViewGroup that will contain other arbitrary views.

Now, the tricky part is how to inflate subsequent views into the content container view.

custom_panel.xml

    <LinearLayout>
        <TextView text="Title" />
        <CardView>
            <LinearLayout id="+@id/contentContainer"></LinearLayout>
        </CardView>
    </LinearLayout>

CustomPanel.java

public class CustomPanel extends LinearLayout {
    // ... Constructor to inflate above layout    
}

activity.xml

<app.CustomPanel>
    <LinearLayout>
        <!-- some other arbitrary views that should be inflated inside content layout -->
    </LinearLayout>
</app.CustomPanel>

I already know how to inflate custom views but I'm looking for, maybe a method, that I can override to add specific child views to the content container (another child) so the former views are not directly descendants on the parent view.

TheRealChx101
  • 1,468
  • 21
  • 38
  • Have a look here: https://stackoverflow.com/a/36947869. Also, since your custom `View` is the `LinearLayout`, the root in `custom_panel` should be `` tags, not another ``. – Mike M. Apr 09 '20 at 17:40
  • @MikeM. Thanks. That worked and was much easier than I anticipated. You wanna submit your comment as an answer? – TheRealChx101 Apr 09 '20 at 18:07
  • Excellent. Glad it worked for ya. That's my answer already, though, so I'd just mark this as a duplicate instead, rather than repeating it. And, actually, I have another that's a little more appropriate, as it explains both the `addView()`, and the ``. Thank you, though. I really appreciate the offer. Cheers! – Mike M. Apr 09 '20 at 18:12

0 Answers0