Search code examples
androidandroid-scrollview

How to Set a Button at a fixed position even when respective Activity has ScrollView


Is there a way to use some non scrollable view when the activity has ScrollView component?

I want that my button behaves similar to the button in the question below, I want it fixed on the bottom of the UI too:

how to set a button at a fixed location on the bottom of UI?

Here's my layout code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent ">

    <FrameLayout
        android:id="@+id/map_frameview"
        android:layout_width="match_parent"
        android:layout_height="200dp">
    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="20dp">
    </LinearLayout>

</LinearLayout>
</ScrollView>

Thanks in advance!


Solution

  • The link you have attached in your question shows how you can do it. You can use RelativeLayout for this. The idea is, you need to tell a button to stay on bottom of the screen & tell the scrollview to stay above the Button along with it's scrollable content like:

    <?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">
    
        <Button
            android:id="@+id/btnMy"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"/>
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/btnMy">
    
            <LinearLayout
                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:layout_above="@id/btnMy"
                android:orientation="vertical">
    
                <FrameLayout
                    android:id="@+id/map_frameview"
                    android:layout_width="match_parent"
                    android:layout_height="200dp">
                </FrameLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingTop="20dp">
                </LinearLayout>
    
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>