user1889890
user1889890

Reputation:

How do I create child views with elements inside?

I came across this video by google that demonstrated animated transitions with the creation of child views by using onClick to add or remove the child view. As part of the learning process: I took the code, broke it down, and made some changes (I am not a copy and paste programmer).

My question is: How do I add a TextView, Button and other various elements to the child view when it is created?

(Project download from google if you're interested: full project)

Here is what I have in my activity:

/**
 * Custom view painted with a random background color and two different sizes which are
 * toggled between due to user interaction.
 */
public class ColoredView extends View {

    private boolean mExpanded = false;

    private LinearLayout.LayoutParams mCompressedParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, 50);

    private LinearLayout.LayoutParams mExpandedParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, 200);

    private ColoredView(Context context) {

        super(context);

        int red = (int)(Math.random() * 128 + 127);
        int green = (int)(Math.random() * 128 + 127);
        int blue = (int)(Math.random() * 128 + 127);
        int color = 0xff << 24 | (red << 16) |
                (green << 8) | blue;
        setBackgroundColor(color);

        setLayoutParams(mCompressedParams);


        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Size changes will cause a LayoutTransition animation if the CHANGING transition is enabled
                setLayoutParams(mExpanded ? mCompressedParams : mExpandedParams);
                mExpanded = !mExpanded;
                requestLayout();

            }
        });
    }
}

I also have this in the onCreate method for showing the animations

LayoutTransition transition = eventContainer.getLayoutTransition();
    transition.enableTransitionType(LayoutTransition.CHANGING);

This is my XML which has the LinearLayout in which all child views are created in:

<LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true"
            android:id="@+id/eventContainer">
    </LinearLayout>

By adding a button or an edittext to the linearlayout it treats it as an entirely new view, only allowing one object per. Is there a way I could slam a bunch of objects into that ONE layout?

I'm also a little confused on the concept of getting the context of views and layouts... I probably misused it somewhere in here.

Upvotes: 0

Views: 330

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82563

You can't, at least not in your current code.

Your ColoredView class currently extends View, which does not support having its own child Views. If you want to add child views to it, you will have to create a class extending ViewGroup (or any of ViewGroup's subclasses).

Once your class extends ViewGroup, you can simple use the addView() method to add Views, and align them by passing LayoutParams (you can also create custom LayoutParams if your view requires complex positioning).

Upvotes: 1

Related Questions