android, adding XML Layout Views to an inflated view in a custom class

i havent found an answer on the internet for quite a while and now im asking you if you can help me.

Short: How should i override addView() (or something else) to add Views defined in XML to my "custom view inflated XML Layout"

Long: I want to create a custom view for my android app, so i created a clean subclass from RelativeLayout. In this, i let the Inflater load a xml layout to get a nice style.

But now, i want to add something inside the custom view, but dont want to add it programattically (this simply works), but with xml. I cant cross the gap in my mind to find the solution...

code: Custom Class:

public class Slider extends RelativeLayout {

    private RelativeLayout _innerLayout;

    public Slider(Context context) {
        super(context);
        init();
    }

    public Slider(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    protected void init() {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        _innerLayout = (RelativeLayout) layoutInflater.inflate(R.layout.layout_r, this);

    }

    @Override
    public void addView(View child) {
        //if (_innerLayout != null) _innerLayout.addView(child);
        super.addView(child);
    }

... all other addView's are overridden in the same way

XML File using the subclass:

<packagename....Slider
    android:id="@+id/slider1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/Red" >
        <TextView
            android:id="@+id/heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HEADING" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="bbb" />

...

The TextView And Button are added to the subclass... sure... but after that, i got 3 children in Slider, the TextView, the Button and my inflated layout from R.layout.layout_r. But i just want 1 child (the layout_r) with Button and TextView in it.

As you can see in addView i tried to simply add the passed "View child" to the _innerLayout. That doesnt work. Android framework keeps calling addView and it ends with StackOverFlowError

Two thing to tell you too:

  1. I know adding Views from XML doesnt call the given addView, but i've overriden all others too and all are looking the same so theres no need to show them.

  2. Debugger said me, that addView is called BEFORE the _innerLayout gets the inflated Layout

Is 2. the reason?

Can u help me?

Upvotes: 0

Views: 1505

Answers (2)

Pavel Shorokhov
Pavel Shorokhov

Reputation: 4974

Just override your addView() method in your custom view Slider and check count of childs. If getChildCount() == 0, then this is first addition and it is view initializing.

Kotlin example:

override fun addView(child: View?, index: Int, params: ViewGroup.LayoutParams?) {
    if (childCount == 0) {
        super.addView(child, index, params)
    } else {
        // Do my own addition
    }
}

Upvotes: 0

marwinXXII
marwinXXII

Reputation: 1446

You can take look on how to inflate children into custom view here (vogella tutorial).

What you need is:

  1. Define layout with children using <merge> tag
  2. Inflate this layout in custom view constructor using LayoutInflater.inflate(res, this, true)

Upvotes: 1

Related Questions