how to handle two layout dynamically android

Viewed 40

I have two layouts vertically in parent layout. First layout has image and second has some relative data. Second layout height can increase according to data. how to manage the first layout height corresponding to second layout. I want to set image background color according to the height of second layout.

<androidx.cardview.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="@dimen/sp_5"
    app:cardElevation="@dimen/margin_5dp"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:id="@+id/parentContainer"
        android:layout_width="match_parent"
        android:layout_height="200sp"
        android:padding="@dimen/sp_5"
        android:layout_marginTop="@dimen/sp_10"
        android:layout_marginBottom="@dimen/sp_10"
        android:background="@drawable/top_rounded_background">

        <RelativeLayout
            android:id="@+id/store_cat_img_container"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <androidx.cardview.widget.CardView
                android:layout_width="150dp"
                android:layout_height="120dp"
                android:layout_centerVertical="true"
                app:cardCornerRadius="@dimen/sp_10"
                app:cardElevation="@dimen/sp_10">

                <ImageView
                    android:id="@+id/productImage"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:src="@mipmap/images" />
            </androidx.cardview.widget.CardView>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/store_cat_img_container"
            android:paddingLeft="@dimen/sp_10">

            <LinearLayout
                android:id="@+id/productNameLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="3">
                <TextView
                    android:id="@+id/productName"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="product Name"
                    android:textColor="@color/black"
                    android:textSize="@dimen/dimen_13dot7sp"
                    android:textStyle="bold"
                    android:layout_weight="1.9"/>

                <TextView
                    android:id="@+id/productOffer"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_marginLeft="@dimen/sp_10"
                    android:layout_toRightOf="@+id/productName"
                    android:layout_weight="1.1"
                    android:text="offer"
                    android:visibility="gone"
                    android:textSize="@dimen/sp_12" />
            </LinearLayout>

            <TextView
                android:id="@+id/productDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/productNameLayout"
                android:text="product description.  product is very nice, good quality product. "
                android:textColor="@color/darkGrey"
                android:textSize="@dimen/sp_10" />

            <LinearLayout
                android:id="@+id/priceQuantLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/productDescription"
                android:orientation="horizontal"
                android:weightSum="3">

                <TextView
                    android:id="@+id/productStrikePrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/productDescription"
                    android:layout_marginRight="@dimen/sp_5"
                    android:layout_toLeftOf="@id/productPrice"
                    android:layout_weight="0.2"
                    android:textColor="@color/darkGrey"
                    android:textSize="@dimen/dimen_15sp"
                    android:visibility="gone" />

                <TextView
                    android:id="@+id/productPrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/productDescription"
                    android:layout_weight="0.2"
                    android:text="\u20B9"
                    android:textColor="@color/dustyred"
                    android:textSize="@dimen/dimen_15sp" />

                <TextView
                    android:id="@+id/productQnt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/productDescription"
                    android:layout_toRightOf="@+id/productPrice"
                    android:text="1 kg"
                    android:textColor="@color/darkGrey"
                    android:textSize="@dimen/sp_12" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/addToCartEditableLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/priceQuantLayout"
                android:backgroundTint="@color/lightGrey"
                android:gravity="center">

                <ImageButton
                    android:id="@+id/minusButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/semiTransparent"
                    android:src="@mipmap/icons_minus" />

                <EditText
                    android:id="@+id/inputCountText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="@dimen/margin_30dp"
                    android:layout_marginRight="@dimen/margin_30dp"
                    android:backgroundTint="@color/white_traspartent"
                    android:inputType="number"
                    android:text="1"
                    android:textColor="@color/sea_green"
                    android:textSize="@dimen/sp_12" />

                <ImageButton
                    android:id="@+id/plusButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:background="@color/semiTransparent"
                    android:src="@mipmap/icons_add" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/addToCartLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:layout_gravity="center"
                android:layout_marginLeft="@dimen/sp_5"
                android:layout_below="@+id/addToCartEditableLayout">

                <Button
                    android:id="@+id/addToCart"
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/dark_orange"
                    android:fitsSystemWindows="true"
                    android:text="Add to cart"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    android:textStyle="bold" />
            </LinearLayout>

        </RelativeLayout>
    </RelativeLayout>
</androidx.cardview.widget.CardView>
1 Answers

I would use ConstraintLayout for overall layout structuring. It is the best and recommended way of handling complex Layout. If you need to control it based on the screen width and height, it has percentage support. Additionally You can also use Guidelines with it.

Related