Search code examples
androidandroid-linearlayoutandroid-buttonandroid-layout-weight

layout weight distribution is not as shown in XML preview


I have the following XML layout -

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".activities.ShoppingCartActivity">

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

        <RelativeLayout
            android:id="@+id/activity_shopping_cart_top_view_linear_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.1"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/activity_shopping_cart_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:text="@string/activity_shopping_cart_shopping_cart"
                android:textSize="20sp"
                android:textStyle="bold" />

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                android:src="@drawable/close_small3x" />

        </RelativeLayout>


        <LinearLayout
            android:id="@+id/activity_shopping_cart_rv_viewgroup"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.45"
            android:orientation="vertical">

            <View
                android:id="@+id/activity_shopping_cart_top_view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/very_light_grey"
                app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_rv_viewgroup" />


            <ProgressBar
                android:id="@+id/activity_shopping_cart_progress_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_top_view" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/activity_shopping_cart_products_rv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_top_view"
                tools:itemCount="4"
                tools:listitem="@layout/marketplace_cart_holder" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/activity_shopping_cart_products_summary_viewgroup"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="@dimen/marketplace_14dp"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:layout_marginEnd="@dimen/marketplace_14dp"
            android:layout_marginBottom="@dimen/marketplace_15dp"
            android:layout_weight="0.45"
            android:orientation="vertical"
            app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_rv_viewgroup">

            <View
                android:id="@+id/activity_shopping_cart_bottom_view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:background="@color/very_light_grey" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:text="@string/activity_shopping_cart_tax_0"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/activity_shopping_cart_tax_0"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:text="@string/activity_shopping_cart_all_amounts_are_in_usd"
                android:textSize="16sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/activity_shopping_cart_shipping_fees_calculated_at_checkout"
                android:textSize="16sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:text="@string/activity_shopping_cart_terms_and_policies"
                android:textSize="12sp"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:background="@color/color_black"
                android:text="@string/activity_shopping_cart_proceed_to_checkout" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:background="@drawable/shopping_cart__clear_background_button"
                android:textColor="@color/color_black"
                android:text="@string/activity_shopping_cart_continue_shopping" />


        </LinearLayout>

    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

which looks like this at the preview -

enter image description here

The issue I am facing is that for some reason, the bottom button "continue shopping" gets shrinked down - enter image description here

Even if I copy the same button downwards below it and replace it with the current one, the copied one will be shrinked to this size.

Here is my clear_background_button.xml file -

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke
        android:width="1dp"
        android:color="@color/red_delete" />
    <solid android:color="@color/white" />


</shape>

What is causing the shrink?


Solution

  • Your layout is not right. Either you have to use NestedScrollView if you have more items or you should redesign your layout like below:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:id="@+id/top_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintTop_toTopOf="parent">
    
            <!-- Top Content -->
    
        </LinearLayout>
    
    
        <LinearLayout
            app:layout_constraintTop_toBottomOf="@+id/top_layout"
            app:layout_constraintBottom_toTopOf="@+id/bottom_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="vertical">
    
            <!-- Other Content -->
    
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/bottom_layout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent">
    
            <!-- Bottom Content -->
    
        </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Output:

    enter image description here