0

I want the cards to fill the entire screen when it comes to various screen dimensions. I would like to use the weights or any other option that can do the same.

        <!--Card 1-->
        <android.support.v7.widget.CardView
            android:id="@+id/cardView1"
            android:layout_height="200sp"
            android:layout_width="150sp">

        </android.support.v7.widget.CardView>

        <!--Card 2-->
        <android.support.v7.widget.CardView
            android:id="@+id/cardView2"
            android:layout_height="200sp"
            android:layout_width="150sp">

        </android.support.v7.widget.CardView>

        <!--Card 3-->
        <android.support.v7.widget.CardView
            android:id="@+id/cardView3"
            android:layout_height="200sp"
            android:layout_width="150sp">

        </android.support.v7.widget.CardView>

        <!--Card 4-->
        <android.support.v7.widget.CardView
            android:id="@+id/cardView4"
            android:layout_height="200sp"
            android:layout_width="150sp">

        </android.support.v7.widget.CardView>
    </GridLayout>

I want the 4 cards to fit the entire screen for any device type

Alex
  • 63
  • 2
  • 9

2 Answers2

0

For this you have to set the height of the gridview dynamically. In your Activity class define.

     public static int heighth;
     public static int width;
     DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        widthh = metrics.widthPixels;
        heighth = metrics.heightPixels;

And after this in your gridview adapter class write

gridView.setMinimumHeight((Home.heighth)/3-70);

Here Home is your Activity class Home.java. I have divided by 3 because I have only 3 rows and the subtraction of 70 is just for adjusting the view. Hope it helps!

  • This doesn't work for devices with on screen menu buttons like Galaxy S9 and so on. The display size would return the whole size of the screen. – juanlugm Jun 14 '19 at 06:26
0

try this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:weightSum="2">

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:weightSum="2">

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

this might cause some performance problems on very old devices, but should not be a problem on the newer ones. Also, just add some margin around the cards to show the shadow if you want hope this helped.

hello_world
  • 778
  • 1
  • 10
  • 26