-2

Ive tried different ways, even did the whole thing again and I don't know why it isn't working. Any help please? whenever I add another card instead of scrolling the view, it just takes more space on the screen decreasing the size of all the other elements there. I'm a beginner so i apologise if i made any stupid mistakes. Before adding another card,After adding another card

<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"
tools:context=".teamAndLeagueSelection"
android:fillViewport="true">

    <RelativeLayout
        android:padding="2dp"
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <Button
            android:id="@+id/doneButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Done"/>
        <Button
            android:id="@+id/logoutButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignLeft="@id/teamSelection"
            android:text="Log out"/>
        <TextView
            android:id="@+id/teamSelection"
            android:text="Select your preferred teams or leagues:"
            android:textSize="25sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/logoutButton"
            />

        <GridLayout
            android:layout_below="@id/teamSelection"
            android:scrollbars="vertical"
            android:columnCount="2"
            android:rowCount="3"
            android:alignmentMode="alignMargins"
            android:columnOrderPreserved="false"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="14dp"
            >
            <!-- Row 1 -->
            <!-- column 1 -->
            <!-- Barcelona -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp"
                >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_barcelona" />

                    <TextView
                        android:layout_gravity="center_horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Barcelona"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <!-- column 2 -->
            <!-- Real Madrid -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp"
                >
                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    >

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/ic_real_madrid" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Real Madrid"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold"/>
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <!-- Row 2 -->
            <!-- column 1 -->
            <!-- Atletico Madrid -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp"
                >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/atletico_madrid" />

                    <TextView
                        android:layout_gravity="center_horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Atletico Madrid"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <!-- column 2 -->
            <!-- Valencia -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp"
                >
                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    >

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/valencia" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Valencia"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold"/>
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <!-- Row 3 -->
            <!-- column 1 -->
            <!-- Getafe CF -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp"
                >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/getafe_cf" />

                    <TextView
                        android:layout_gravity="center_horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Getafe CF"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <!-- column 2 -->
            <!-- Sevilla -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp"
                >
                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    >

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/sevilla" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Sevilla"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold"/>
                </LinearLayout>
            </android.support.v7.widget.CardView>

        </GridLayout>
    </RelativeLayout>
</ScrollView>
  • @Zoe I am using android studio for my project. This is a part of my project not just some code. – Ali Rathore Feb 25 '19 at 06:26
  • So? You're not asking about AS specifically, and the code would still be identical whether you used Android Studio, Eclipse, or hacked together something and used Vim. See https://meta.stackoverflow.com/a/315196/6296561 – Zoe Feb 25 '19 at 06:32
  • @Zoe when should I use the tag then? can you please give me an example? – Ali Rathore Feb 25 '19 at 06:47
  • Use the android tag instead (you already used it, so the only thing needed was removing the Android Studio tag). See also the [tag wiki](https://stackoverflow.com/tags/android-studio/info) – Zoe Feb 25 '19 at 06:52

3 Answers3

0

Instead of Using GridLayout use RecyclerView and User Adapter for adding card dynamically

0

you have provide the android:layout_rowWeight="1" in cardview remove that line and add height to android:layout_height="wrap_content" or some value like android:layout_height="250dp" in your all Cardviews.
There is not a problem with ScrollView.

-1

You have to put your scroll view inside the layout then it will working fine...

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

<ScrollView android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
xmlns:android="http://schemas.android.com/apk/res/android">

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

  <!--put your nested layouts and other things which you want to use for 
  scrolling-->

</LinearLayout>
 </ScrollView>
   </LinearLayout>

Just remember this that scroll view only allows one layout inside it.

the above code will definitely help you

Vipul Chauhan
  • 189
  • 4
  • 19