How to make 4 CardView using RelativeLayout , which resize and occupy 25% of the screen, regardless of filling

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:padding="@dimen/padding_all"> <android.support.v7.widget.CardView android:id="@+id/understockCard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/card_margin" android:layout_weight="0.5" card_view:cardCornerRadius="@dimen/overview_cards_corner_radius" card_view:cardElevation="@dimen/overview_cards_cardElevation" card_view:contentPadding="@dimen/overview_cards_contentPadding"> <TextView android:id="@+id/t1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UNDERSTOCK" android:textAppearance="?android:attr/textAppearanceLarge" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:id="@+id/nearReorderCard" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/card_margin" android:layout_weight="0.5" android:layout_toRightOf="@+id/understockCard" card_view:cardCornerRadius="@dimen/overview_cards_corner_radius" card_view:cardElevation="@dimen/overview_cards_cardElevation" card_view:contentPadding="@dimen/overview_cards_contentPadding"> <TextView android:id="@+id/t12" android:layout_width="100dp" android:layout_height="wrap_content" android:text="NEAR REORDER" android:textAppearance="?android:attr/textAppearanceLarge" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:id="@+id/properLevelsCard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/card_margin" android:layout_weight="0.5" android:layout_below="@+id/understockCard" card_view:cardCornerRadius="@dimen/overview_cards_corner_radius" card_view:cardElevation="@dimen/overview_cards_cardElevation" card_view:contentPadding="@dimen/overview_cards_contentPadding"> <TextView android:id="@+id/t3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="PROPER LEVELS" android:textAppearance="?android:attr/textAppearanceLarge" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:id="@+id/overstockCard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/card_margin" android:layout_weight="0.5" android:layout_below="@+id/nearReorderCard" android:layout_toRightOf="@+id/properLevelsCard" card_view:cardCornerRadius="@dimen/overview_cards_corner_radius" card_view:cardElevation="@dimen/overview_cards_cardElevation" card_view:contentPadding="@dimen/overview_cards_contentPadding"> <TextView android:id="@+id/t4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="OVERSTOCK" android:textAppearance="?android:attr/textAppearanceLarge" /> </android.support.v7.widget.CardView> </RelativeLayout> 
  • And he did. Write the answer, and I will accept it - Igor

2 answers 2

There is such a support library from Google - Percent Support Library . It implements container classes in which you can specify distances and sizes for nested elements as a percentage of the size of the container itself.
The PercentRelativeLayout class from this library will solve your problem most easily.

    Of course a strange desire. Why not take LinearLayout and distribute the same layout_weight to everyone with android:layout_height="0dp" ?

    But if such a desire to do everything on RelativeLayout right, the first thing that came to mind: we place an invisible view in the center and dance from it. Markup and result:

    enter image description here

    All card_view:cardCornerRadius="@dimen and stuff removed, atoms and so much text. Add the necessary.

      <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_above="@+id/centerRLRoot"> <View android:id="@+id/centerRLOne" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true"/> <android.support.v7.widget.CardView android:id="@+id/understockCard" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/centerRLOne" android:layout_alignParentTop="true"> <TextView android:id="@+id/t1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UNDERSTOCK" android:textAppearance="?android:attr/textAppearanceLarge" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:id="@+id/understockCard2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_below="@+id/understockCard"> <TextView android:id="@+id/t2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UNDERSTOCK" android:textAppearance="?android:attr/textAppearanceLarge" /> </android.support.v7.widget.CardView> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/centerRLRoot" android:layout_centerHorizontal="true"> <View android:id="@+id/centerRLTwo" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true"/> <android.support.v7.widget.CardView android:id="@+id/understockCard3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/centerRLTwo" android:layout_alignParentTop="true"> <TextView android:id="@+id/t3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UNDERSTOCK" android:textAppearance="?android:attr/textAppearanceLarge" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:id="@+id/understockCard4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_below="@+id/understockCard3"> <TextView android:id="@+id/t4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UNDERSTOCK" android:textAppearance="?android:attr/textAppearanceLarge" /> </android.support.v7.widget.CardView> </RelativeLayout> </RelativeLayout>