Scroll View android studio

28 Views Asked by At

For some reason I am not able to figure out how to create the scroll affect for an android app in android studio. I've looked at videos copied the same style asked chatgpt everything and I cant seem to figure out how.

this is the code below if anyone could help

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:padding="1dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:overScrollMode="always"
        android:scrollbars="vertical">

        <LinearLayout
            android:id="@+id/fullscreen_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="0dp"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/top_profile_bar_linear"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:weightSum="10"
                android:baselineAligned="false">
                <LinearLayout
                    android:id="@+id/profile_to_home_button"
                    android:layout_width="90dp"
                    android:layout_height="match_parent"
                    android:clickable="true"
                    android:focusable="true"
                    android:layout_weight="0.1"
                    >
                    <ImageView
                        android:id="@+id/back_image"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:paddingTop="2dp"
                        android:src="@drawable/baseline_arrow_back_24" />
                    <TextView
                        android:id="@+id/back_text"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:paddingBottom="2dp"
                        android:text="@string/home"
                        android:textColor="@color/Neon_purple"
                        android:textSize="17dp"
                        android:textStyle="bold"
                        android:paddingLeft="5dp"
                        android:paddingTop="2dp"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="300dp"
                    android:layout_weight="9.9"
                    android:layout_height="match_parent"
                    android:layout_marginRight="10dp">
                    <ImageView
                        android:id="@+id/notification_button"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:clickable="true"
                        android:focusable="true"
                        android:src="@drawable/baseline_all_inbox_24"
                        android:paddingLeft="270dp">
                    </ImageView>
                </LinearLayout>
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/user_photo_and_details"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_below="@+id/top_profile_bar_linear"
                android:layout_marginTop="0dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:weightSum="100">
                    <LinearLayout
                        android:id="@+id/profile_picture_linear"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_marginTop="50dp"
                        android:layout_marginLeft="25dp"
                        >
                        <ImageView
                            android:id="@+id/user_image"
                            android:layout_width="100dp"
                            android:layout_height="100dp"
                            android:src="@drawable/rohan_image_circle">
                        </ImageView>
                    </LinearLayout>
                    <RelativeLayout
                        android:layout_width="150dp"
                        android:layout_height="40dp"
                        android:layout_marginTop="60dp"
                        android:layout_marginLeft="10dp">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:paddingLeft="5dp"
                            android:paddingTop="2dp"
                            android:text="@string/user_details"
                            android:textColor="@color/black"
                            android:textSize="20dp"
                            android:textStyle="bold"></TextView>
                    </RelativeLayout>
                    <RelativeLayout
                        android:id="@+id/edit_profile_button"
                        android:layout_width="50dp"
                        android:layout_height="40dp"
                        android:layout_marginTop="60dp"
                        android:layout_marginStart="50dp"
                        android:focusable="true"
                        android:clickable="true">
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:src="@drawable/pen_profile"
                            android:paddingEnd="10dp">
                        </ImageView>
                    </RelativeLayout>
                    <RelativeLayout
                        android:layout_width="200dp"
                        android:layout_height="40dp"
                        android:layout_marginTop="90dp"
                        android:layout_marginLeft="-245dp">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:textSize="16dp"
                            android:text="View user info quickly">
                        </TextView>
                    </RelativeLayout>
                </LinearLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/edit_options_text"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_below="@+id/user_photo_and_details"
                android:layout_marginTop="-48dp">
                <LinearLayout
                    android:layout_width="200dp"
                    android:layout_height="30dp"
                    android:layout_marginLeft="30dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="Edit Options"
                        android:textSize="20dp">
                    </TextView>
                </LinearLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/option_buttons"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:layout_below="@+id/edit_options_text"
                android:layout_marginTop="0dp">
                <LinearLayout
                    android:id="@+id/manageAccountButton"
                    android:layout_width="match_parent"
                    android:layout_height="75dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:orientation="horizontal">
                    <RelativeLayout
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:layout_marginStart="25dp">
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:src="@drawable/user_settings"
                            android:contentDescription="user Settings">
                        </ImageView>
                    </RelativeLayout>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/managePaymentButton"
                    android:layout_width="match_parent"
                    android:layout_height="75dp"
                    android:layout_alignTop="@id/manageAccountButton"
                    android:layout_marginTop="75dp"
                    android:clickable="true"
                    android:focusable="true">
                    <RelativeLayout
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:layout_marginLeft="25dp">
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:src="@drawable/credit_card_profile">
                        </ImageView>
                    </RelativeLayout>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/watchlistButton"
                    android:layout_width="match_parent"
                    android:layout_height="75dp"
                    android:layout_alignTop="@id/managePaymentButton"
                    android:layout_marginTop="75dp"
                    android:clickable="true"
                    android:focusable="true">
                    <RelativeLayout
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:layout_marginStart="25dp">
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:src="@drawable/watch_list"
                            android:contentDescription="Watchlist" />
                    </RelativeLayout>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/supportButton"
                    android:layout_width="match_parent"
                    android:layout_height="75dp"
                    android:layout_below="@+id/watchlistButton"
                    android:layout_marginTop="-1dp"
                    android:clickable="true"
                    android:focusable="true"
                    tools:ignore="NotSibling">

                    <RelativeLayout
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:layout_marginStart="25dp">
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:contentDescription="Watchlist"
                            android:src="@drawable/watch_list" />
                    </RelativeLayout>
                </LinearLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/signout_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/option_buttons"
                android:layout_marginBottom="-144dp"
                android:gravity="center">
                <LinearLayout
                    android:id="@+id/signoutButton"
                    android:layout_width="350dp"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:focusable="true"
                    android:orientation="horizontal"
                    android:layout_marginTop="50dp"
                    android:background="@drawable/sign_out_card"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                        android:text="Sign out"
                        android:textSize="30dp"
                        android:textColor="@color/white">

                    </TextView>

                </LinearLayout>

            </RelativeLayout>

        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

even though the signout block would be out of screen it wont let me scroll.

1

There are 1 best solutions below

1
ganjaam On

The negative value of marginBottom in signout_layout is the problem. The scroll seems to be working well. But because of setting -144dp as the margin, the scroll view is not only hiding the sign out layout, but also not letting user to scroll. It's because it's thinking that it has already reached the bottom.

It would be okay to use negative value for margin bottom if this layout were in the top or in the middle of the screen. However, since it's in the bottom, this problem is occurring.

I'd recommend not to use marginBottom at all because it seems redundant. Even if you do, use a non negative value. And if you want to hide the view, use android:visibility="gone".

            <RelativeLayout
                android:id="@+id/signout_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/option_buttons"
                android:visibility="gone"
                android:gravity="center">
                <!--                android:layout_marginBottom="-144dp"-->

Also, your code can be optimised a lot! For example, in some parts, you coded like this:

<RelativeLayout>
    <LinearLayout>
        <TextView/>
    </LinearLayout>
</RelativeLayout>

It can be simplified to the following:

<RelativeLayout>
    <TextView>
<RelativeLayout>

This will not only boost the efficiency of the app, but also make it easy for others to read your code.