Watson
Watson

Reputation: 3

layout weight not working CardView

I want my screen to show a total of 3 cards distributed evenly taking up the whole space but nothing I try seems to be working. The cards are being loaded with the use of a RecyclerView/viewHolder. I've tried two solutions.

Solution 1:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    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="wrap_content"
    android:id="@+id/card_view_disciplines"
    card_view:cardCornerRadius="4dp"

    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3"
    >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/disciplineName"
            android:textSize="25sp"
            android:layout_weight="1"
            />
</LinearLayout>
    </RelativeLayout>

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

and Solution 2:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    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="wrap_content"
    android:id="@+id/card_view_disciplines"
    card_view:cardCornerRadius="4dp"

    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3"
    >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/disciplineName"
            android:textSize="25sp"
            android:layout_weight="1"
            />
</LinearLayout>
    </RelativeLayout>

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

can someone please help?

Thanks in advance!

Upvotes: 0

Views: 4396

Answers (2)

Sabari
Sabari

Reputation: 1981

I hope this will work as you expected:

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#888888"
    android:orientation="vertical"
    android:weightSum="3">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:layout_weight="1"
        card_view:cardBackgroundColor="#000088"
        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="5dp">


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

    <android.support.v7.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:layout_weight="1"
        card_view:cardBackgroundColor="#008800"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="5dp">

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

    <android.support.v7.widget.CardView
        android:id="@+id/cardView3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:layout_weight="1"
        card_view:cardBackgroundColor="#880000"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="5dp">

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

MainActivity.java

CardView cardView1 = (CardView) findViewById(R.id.cardView1);
        CardView cardView2 = (CardView) findViewById(R.id.cardView2);
        CardView cardView3 = (CardView) findViewById(R.id.cardView3);

        cardView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "CardView1", Toast.LENGTH_SHORT).show();
            }
        });

        cardView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "CardView2", Toast.LENGTH_SHORT).show();
            }
        });

        cardView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "CardView3", Toast.LENGTH_SHORT).show();
            }
        });

Upvotes: 1

Jefferson Tavares
Jefferson Tavares

Reputation: 991

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:weight_sum="3"
    android:padding="16dp">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            app:cardBackgroundColor="#000">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="40sp"
                android:text="Card 1"
                android:textColor="#FFF"
                android:layout_gravity="center"/>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:padding="16dp"
            android:background="#000">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="40sp"
                android:text="Card 2"
                android:layout_gravity="center"/>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:cardBackgroundColor="#000">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="40sp"
                android:text="Card 3"
                android:textColor="#FFF"
                android:layout_gravity="center"/>
    </android.support.v7.widget.CardView>
    </LinearLayout>

Result

Upvotes: 1

Related Questions