Learn ScrollView in Android using step by step examples.

Example 1: Simple ScrollView

A simple ScrollView example.

Step 1: Create Project

The first step is to create a Android Project in Android Studio.

Step 2: Add Dependencies

No external dependencies are needed for this project.

Step 3: Design Layouts

We need only one layout:

main.xml

Inside it add two buttons, a textview and a ScrollView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/up"
        android:text="Up"/>
    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">
    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </ScrollView>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/down"
        android:text="Down"/>
</LinearLayout>

Step : Write Code

Here’s the full code:

MainActivity.java


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private  TextView tv;
    private ScrollView scrollView;
    private Button upbtn;
    private Button downtb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super . onCreate (savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.content);
        //tv.setText(R.string.content);
        tv.setText(getResources().getString(R.string.content));
        upbtn = (Button) findViewById(R.id.up);
        downtb = (Button) findViewById(R.id.down);

        upbtn.setOnClickListener(this);
        downtb.setOnClickListener(this);

        scrollView = (ScrollView) findViewById(R.id.scroll);
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP: {
                        break;
                    }
                    case MotionEvent.ACTION_MOVE: {
                        /**
                         * (1) getScrollY() --The distance the scroll bar slides
                         * (2) getMeasuredHeight-- These two values ​​are equal when one screen is displayed
                         * (3) getHeight--
                         */

                        // Top state
                        if (scrollView.getScrollY() <= 0) {
                            Log.i("Main", "顶部");
                        }
                        // Bottom status
                        // The total height of textView = the height of a screen + the scroll distance of the scroll bar
                        if (scrollView.getChildAt(0).getMeasuredHeight() <= scrollView.getHeight() + scrollView.getScrollY()) {
                            Log . I( " Main " , " Slide to the bottom " );
                            Log.i("Main", "scrollView.getChildAt(0).getMeasuredHeight()" + scrollView.getChildAt(0).getMeasuredHeight());
                            Log.i("Main", "scrollView.getHeight()" + scrollView.getHeight());
                            Log.i("Main", "scrollView.getScrollY()" + scrollView.getScrollY());
                        }
                        tv.append(getResources().getString(R.string.content));
                        break;
                    }
                    case MotionEvent.ACTION_DOWN: {
                        break;
                    }
                }
                return false;
            }
        });
    }

    @Override
    public  void  onClick ( View  v ) {
        switch (v.getId()){
            // scrollTo: Start counting from the starting position of the scroll view
            // scrollBy: the distance of the previous scroll to scroll

            case R.id.up:{
                //scrollView.scrollTo(0,-30);
                scrollView.scrollBy(0, -30);
                break;
            }
            case R.id.down:{
                //scrollView.scrollTo(0,30);
                scrollView.scrollBy(0,30);
                break;
            }
        }
    }
}

Run

Copy the code into your project or download the code in the reference links below.

Reference

Find the reference links below:

Number Link
1. Download code
2. Follow code author