a874 a874 - 4 months ago 16
Android Question

Cards not getting aligned correctly

I am trying to have four cards.I want first two cards be horizontally aligned next to each other and then two cards need to be below and also to be aligned horizontally next to each other.

I am using two separate

LinearLayout
each for a row of two cards(i.e two cards in each row).

The problem is that only first two cards are visible and other two cards are not visible.


XML code


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="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:orientation="vertical"
tools:context=".MainActivity"
android:background="#bcd4d4"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="20dp"
>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view1"
android:layout_width="180"
android:layout_height="200dp"
card_view:cardCornerRadius="10dp"
android:foregroundGravity="center"
android:layout_marginBottom="20dp"
card_view:cardElevation="15dp"
card_view:cardBackgroundColor="#52bf90"
card_view:cardUseCompatPadding="true"
android:onClick="missed"

>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="MISSED"
android:textSize="35sp"
android:textAllCaps="true"
android:textAlignment="gravity"
android:layout_marginTop="40dp"
>

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

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view2"
android:layout_width="180dp"
android:layout_height="200dp"
card_view:cardCornerRadius="10dp"
android:foregroundGravity="center"
card_view:cardElevation="15dp"
card_view:cardBackgroundColor="#52bf90"
card_view:cardUseCompatPadding="true"
android:onClick="received"
>

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="RECEIVED"
android:textSize="30sp"
android:textAllCaps="true"
android:layout_marginTop="40dp"
>
</TextView>

<!--/>-->
</android.support.v7.widget.CardView>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view3"
android:layout_width="180dp"
android:layout_height="200dp"
card_view:cardCornerRadius="10dp"
android:foregroundGravity="center"

card_view:cardElevation="15dp"
card_view:cardBackgroundColor="#52bf90"
card_view:cardUseCompatPadding="true"
android:onClick="dialled"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="DIALLED"
android:textSize="40sp"
android:textAllCaps="true"
android:layout_marginTop="40dp"
>
</TextView>

</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view4"
android:layout_width="180dp"
android:layout_height="200dp"
card_view:cardCornerRadius="10dp"
android:foregroundGravity="center"

card_view:cardElevation="15dp"
card_view:cardBackgroundColor="#52bf90"
card_view:cardUseCompatPadding="true"
android:onClick="stats"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="STATS"
android:textSize="40sp"
android:layout_marginTop="40dp"
android:textAllCaps="true"
>
</TextView>
</android.support.v7.widget.CardView>
</LinearLayout>



Answer

You need to set your both horizontal <LinearLayout> android:layout_height="wrap_content"

like as

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="20dp"
   >
<android.support.v7.widget.CardView..........