0
votes

I am trying to populate a Relative layout with 4 card views in it. Each cardview consists one image and a text view. I wrapped all the card views in a relative layout and the relative layout in a scroll view. It's not working. If I remove scrollview, its working fine. Tried by putting scrollview inside the relative layout. But didnt work Code:

    <?xml version="1.0" encoding="utf-8"?>
    <Scrollview
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:fillViewport="true"
     android:layout_height="match_parent">

   <RelativeLayout
      android:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#659D32"
      android:paddingBottom="16dp"
      android:paddingLeft="16dp"
      android:paddingRight="16dp"
      android:orientation="vertical"
      android:paddingTop="16dp"
      tools:context="com.example.MainActivity">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:cardCornerRadius="5dp"
    android:id="@+id/cardview_1"
    app:cardElevation="10dp"
    app:cardMaxElevation="15dp">

    <RelativeLayout
        android:layout_width="match_parent"

        android:layout_height="match_parent">


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:src="@drawable/img_1"
            android:id="@+id/cardImage_bang"
            android:scaleType="centerCrop"/>

        <TextView
            android:layout_below="@+id/cardImage_1"
            android:id="@+id/cardTitle_1"
            android:gravity="center_horizontal"
            android:text="Text1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

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

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:cardCornerRadius="5dp"
    android:layout_below="@+id/cardview_1"
    android:id="@+id/cardview_2"
    app:cardElevation="10dp"
    android:layout_marginTop="10dp"

    app:cardMaxElevation="15dp">

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


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:src="@drawable/img_2"
            android:id="@+id/cardImage_2"
            android:scaleType="centerCrop"/>

        <TextView
            android:layout_below="@+id/cardImage_2"
            android:id="@+id/cardTitle_2"
            android:gravity="center_horizontal"
            android:text="Text2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

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

3
try to put wrap_content in RelativeLayout height.Yupi
ScrollView contains only one child, so make ensure that your all things should be inside the your first relative layout.ariful islam arif

3 Answers

0
votes

Hope this will help you.. (this snippet worked in my case)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="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:fillViewport="true"
    android:paddingLeft="5dp"
    android:background="@color/bg"
    android:paddingRight="5dp">

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical"
            android:padding="10dp"
            android:weightSum="1">

        <!-- put entire thing here -->

    </RelativeLayout>
</ScrollView>
0
votes

you have written the scroll view tag wrong. That's the reason its not working. Change it from <Scrollview> to <ScrollView> and add the Scroll view end tag like </ScrollView>and re run the project, it will work fine.

Hope this will help you !

0
votes

1) You've misspelled the ScrollView tag it must be <ScrollView> not <Scrollview>

2) You didn't close the <ScrollView> tag at the end of the code

-->Here's the right code

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:fillViewport="true"
 android:layout_height="match_parent">

<RelativeLayout
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#659D32"
  android:paddingBottom="16dp"
  android:paddingLeft="16dp"
  android:paddingRight="16dp"
  android:orientation="vertical"
  android:paddingTop="16dp"
  tools:context="com.example.MainActivity">

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="5dp"
android:id="@+id/cardview_1"
app:cardElevation="10dp"
app:cardMaxElevation="15dp">

 <RelativeLayout
    android:layout_width="match_parent"

    android:layout_height="match_parent">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:src="@drawable/img_1"
        android:id="@+id/cardImage_bang"
        android:scaleType="centerCrop"/>

    <TextView
        android:layout_below="@+id/cardImage_1"
        android:id="@+id/cardTitle_1"
        android:gravity="center_horizontal"
        android:text="Text1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="5dp"
android:layout_below="@+id/cardview_1"
android:id="@+id/cardview_2"
app:cardElevation="10dp"
android:layout_marginTop="10dp"

app:cardMaxElevation="15dp">

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


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:src="@drawable/img_2"
        android:id="@+id/cardImage_2"
        android:scaleType="centerCrop"/>

    <TextView
        android:layout_below="@+id/cardImage_2"
        android:id="@+id/cardTitle_2"
        android:gravity="center_horizontal"
        android:text="Text2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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