Search code examples
androidandroid-layoutandroid-appbarlayout

How to make App Bar and an Edittext fixed at the top of the layout in Android


I have this layout :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    xmlns:app="http://schemas.android.com/apk/res-auto">




    <LinearLayout 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:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_show" tools:context="com.example.bimpc1.myapp.ShowActivity"
        android:background="@color/backgrndclr"
        >


        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            xmlns:android="http://schemas.android.com/apk/res/android" />
        <View
            android:layout_width="fill_parent"
            android:layout_height="30dp">
        </View>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">




            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/et_word"
                android:ems="14"
                android:hint="Search"
                android:drawableBottom="@color/bottomcolor"/>

            <ImageButton
                android:id="@+id/btn_bring"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:src="@drawable/search"
                android:background="@color/buttonbckgrndclr"
                android:paddingRight="10dp"
                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:paddingBottom="5dp"
                />

        </LinearLayout>
<!-- And lots of other view elements -->
    </LinearLayout>
</ScrollView>

And it looks like this on screen :

enter image description here

I want that bar and that search edit text and search button to be fixed at the top of the screen. I mean they shouldn't disappear when user scrolls down. How can I achieve that? I googled my problem but I couldn't find a solution. Thanks.


Solution

  • Move ScrollView below your two Elements:

    <LinearLayout 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:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_show" tools:context="com.example.bimpc1.myapp.ShowActivity"
        android:background="@color/backgrndclr"
        >
    
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            xmlns:android="http://schemas.android.com/apk/res/android" />
        <View
            android:layout_width="fill_parent"
            android:layout_height="30dp">
        </View>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">
    
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/et_word"
                android:ems="14"
                android:hint="Search"
                android:drawableBottom="@color/bottomcolor"/>
    
            <ImageButton
                android:id="@+id/btn_bring"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:src="@drawable/search"
                android:background="@color/buttonbckgrndclr"
                android:paddingRight="10dp"
                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:paddingBottom="5dp"
                />
    
        </LinearLayout>
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:fillViewport="true"
            xmlns:app="http://schemas.android.com/apk/res-auto">    
    
            <!-- And lots of other view elements -->
            </ScrollView>
    </LinearLayout>