English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Android Click Button Return to Top Implementation Code

click the button to return to the top, let's dive into the code directly

layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ffffff"
  android:orientation="vertical">
  <ScrollView
    android:id="@"+id/sv_home"
    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.support.design.widget.FloatingActionButton
        android:id="@"+id/fab_top"
        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

topBtn.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
      sc.post(new Runnable() {
        @Override
        public void run() {
         sc.post(new Runnable() {
           public void run() {
            // Back to Top
            sc.fullScroll(ScrollView.FOCUS_UP);
           }
         });
        }
      });
     }
   });

Attach a jump to the bottom

 bottomBtn.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
      sc.post(new Runnable() {
        @Override
        public void run() {
         sc.post(new Runnable() {
           public void run() {
            // Scroll to the bottom
            sc.fullScroll(ScrollView.FOCUS_DOWN);
           }
         });
        }
      });
     }
   });

That's all for the content of this article. Hope it helps your learning and also hope everyone will support the Yelling Tutorial more.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When sending an email, please replace # with @ to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like