android - How to create a Scrollable TextView

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/rl"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     tools:context=".MainActivity"     android:background="#e5edf1"     >     <TextView         android:id="@+id/tv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Line One\nLine Two\nLine Three\nLine Four\nLine Five\nLine Six"         android:background="#fdfbd0"         android:textSize="18dp"         android:layout_margin="10dp"         android:padding="10dp"         />     <!--         android:maxLines             Makes the TextView be at most this many lines tall. When used on an editable text, the             inputType attribute value must be combined with the textMultiLine flag for the             maxLines attribute to apply.              Must be an integer value, such as "100".              This may also be a reference to a resource (in the form "@[package:]type:name") or theme             attribute (in the form "?[package:][type:]name") containing a value of this type.              This corresponds to the global attribute resource symbol maxLines.     -->     <TextView         android:id="@+id/tv_second"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Line One\nLine Two\nLine Three\nLine Four\nLine Five\nLine Six"         android:background="#eaa3a3"         android:maxLines="3"         android:scrollbars="vertical"         android:textSize="18dp"         android:layout_margin="10dp"         android:padding="10dp"         android:layout_toRightOf="@id/tv"         />     <TextView         android:id="@+id/tv_third"         android:layout_width="wrap_content"         android:layout_height="75dp"         android:text="Line One\nLine Two\nLine Three\nLine Four\nLine Five\nLine Six"         android:background="#a3eabb"         android:scrollbars="vertical"         android:textSize="18dp"         android:layout_toRightOf="@id/tv_second"         android:layout_margin="10dp"         android:padding="10dp"         /> </RelativeLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.app.Activity; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; import android.widget.RelativeLayout; import android.widget.TextView;   public class MainActivity extends AppCompatActivity {      private Context mContext;     private Activity mActivity;      private RelativeLayout mRelativeLayout;     private TextView mTextViewSecond;     private TextView mTextViewThird;       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the application context         mContext = getApplicationContext();          // Get the activity         mActivity = MainActivity.this;          // Get the widgets reference from XML layout         mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);         mTextViewSecond = (TextView) findViewById(R.id.tv_second);         mTextViewThird = (TextView) findViewById(R.id.tv_third);          /*             public akibat void setMovementMethod (MovementMethod movement)                 Sets the movement method (arrow key handler) to be used for this TextView. This can                 be null to disallow using the arrow keys to move the cursor or scroll the view.                  Be warned that if you want a TextView with a key listener or movement method not to                 be focusable, or if you want a TextView without a key listener or movement method to                 be focusable, you must call setFocusable(boolean) again after calling this to get                 the focusability back the way you want it.              Parameters                 movement : MovementMethod         */         /*             MovementMethod                 Provides cursor positioning, scrolling and text selection functionality in a TextView.                  The TextView delegates handling of key events, trackball motions and touches to the                 movement method for purposes of content navigation. The framework automatically                 selects an appropriate movement method based on the content of the TextView.                  This interface is intended for use by the framework; it should not be implemented                 directly by applications.         */          // Set the scrolling movement method for TextView         mTextViewThird.setMovementMethod(new ScrollingMovementMethod());         mTextViewSecond.setMovementMethod(new ScrollingMovementMethod());     } } 

Komentar