SoFunction
Updated on 2025-04-10

Android click the button to return to the top to implement the code

Click the button to go back to the top and go directly to the code

Layout file

<LinearLayout xmlns:andro
  xmlns:app="/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ffffff"
  android:orientation="vertical">

  <ScrollView
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
      <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="@mipmap/eason"/>

      <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="@mipmap/eason"/>

      <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="@mipmap/eason"/>
                    <
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_marginRight="10dp"
        android:src="@mipmap/top"
        app:backgroundTint="#ecefef"
        app:elevation="10dp"
        app:pressedTranslationZ="12dp"
        app:rippleColor="@color/colorPrimary" />
    </LinearLayout>
  </ScrollView>
</LinearLayout>

Button click event

(new OnClickListener() {

     @Override
     public void onClick(View v) {
      (new Runnable() {

        @Override
        public void run() {
         (new Runnable() {
           public void run() {
            // Back to top            (ScrollView.FOCUS_UP);
           }
         });
        }
      });

     }
   });

Comes with one to jump to the bottom

 (new OnClickListener() {

     @Override
     public void onClick(View v) {
      (new Runnable() {

        @Override
        public void run() {
         (new Runnable() {
           public void run() {
            // Scroll to the bottom            (ScrollView.FOCUS_DOWN);
           }
         });
        }
      });
     }
   });

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.