Zander B
Zander B

Reputation: 247

Make a view fill a wrap content parent layout

What it should look like Screenshot of the problem

I am trying to add color coding of classes for my android app which organized HW. I am trying to do this by adding a bar on the side with the color of the class. In the image I want the whole layout to wrap the height of the text and then have Shape A fill that layout. I tried to used match parent on shape A which didn't work. After that, what I tried to do was programmatically measure the relative layout and set shape A to the height of that which worked all the time except when the text has to wrap to two lines. All help is appreciated. Thanks!

XML Code: The TV with id side_bar is the side bar I am trying to add.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<RelativeLayout
    android:id="@+id/rel_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF">


    <RelativeLayout
        android:id="@+id/check_layout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerVertical="true">

        <change.com.puddl.CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:background="#4CAF50" />

        <ImageView
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:src="@drawable/ic_check_white_24dp"
            android:tint="#4CAF50"
            android:visibility="gone" />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/check_layout"
        android:background="#FFFFFF"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="4dp"
            android:text="This is a summary..."
            android:textColor="#727272"
            android:textSize="20dp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp">

            <TextView
                android:id="@+id/Due"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:text="Due"
                android:textColor="#888"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/Course"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:text="Course"
                android:textSize="12sp" />

        </RelativeLayout>


    </LinearLayout>

    <TextView
        android:id="@+id/side_bar"
        android:layout_width="4dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:background="#727272" />

</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#DDDDDD" />

<View
    android:id="@+id/topShadow"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#DDDDDD"
    android:visibility="gone" />
</LinearLayout>

Upvotes: 0

Views: 1552

Answers (1)

Jemshit
Jemshit

Reputation: 10038

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row_base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@color/black">

    <!--your shape-->
    <LinearLayout
        android:layout_width="15dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/redDark">
    </LinearLayout>

    <!--check icon-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/gray_light1">

        <ImageView
            android:id="@+id/check"
            android:layout_gravity="center_vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:src="@drawable/checked"
            android:tint="#4CAF50"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>

    <!--COntent, texts-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/gray_light3">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:text="This is a summary..."
            android:textColor="@color/black"
            android:textSize="20dp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/gray_light2">

                    <TextView
                        android:id="@+id/Due"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentLeft="true"
                        android:text="Due"
                        android:textColor="@color/black"
                        android:textSize="12sp" />

                    <TextView
                        android:id="@+id/Course"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentRight="true"
                        android:text="Course"
                        android:textColor="@color/black"
                        android:textSize="12sp" />

        </RelativeLayout>


    </LinearLayout>

</LinearLayout>

enter image description here

When you use this as list item, it will have good vision.

Upvotes: 1

Related Questions