Andrew Hamili
Andrew Hamili

Reputation: 53

Single ListView inside a CardView

I have a screen requirement like this:

Screen Requirement

But I cannot get it to work like that. I ended up making a View like this:

Actual

I am using a CardView. The List content is dynamically expanding.

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

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/lblProductName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="19dp"
                android:padding="10dp"
                android:text="Product Name"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/lblProductQuantity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="19dp"
                android:padding="10dp"
                android:text="99"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>

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

</RelativeLayout>

How can I make my screen to look the same as the required screen?

Upvotes: 3

Views: 3495

Answers (3)

Vijay Chaudhary
Vijay Chaudhary

Reputation: 228

Put your List View inside Card View

<android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            card_view:cardCornerRadius="2dp"
            card_view:contentPadding="10dp">
    <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical" />
    </android.support.v7.widget.CardView>

Upvotes: 0

yogesh lokhande
yogesh lokhande

Reputation: 1275

Add Your Recyclerview inside card view

<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:id="@+id/cv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@color/color"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardBackgroundColor="#fff"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="3.5dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recylerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

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

and change your adapter layout as follows

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


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/lblProductName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="19dp"
                android:padding="10dp"
                android:text="Product Name"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/lblProductQuantity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="19dp"
                android:padding="10dp"
                android:text="99"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>


</RelativeLayout>

Upvotes: 1

Vishal Vaishnav
Vishal Vaishnav

Reputation: 3422

Try like below code in xml , put your ListView inside CardView;

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:background="@color/color"
        android:id="@+id/cv1"
        card_view:cardElevation="3.5dp"
        card_view:cardBackgroundColor="#fff"
        card_view:cardCornerRadius="3dp"
        android:foreground="?android:attr/selectableItemBackground"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

           <ListView
            android:id="@+id/study_level_list"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        </LinearLayout>

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

Upvotes: 5

Related Questions