Saturday, August 27, 2016

TextView, auto scroll down to display bottom of text


This example show how to make a TextView auto scroll down to display bottom of text. In the demonstration, the upper TextView is normal, user cannot see the bottom of text if it is full. The lower one, the TextView will auto scroll down, such that user can see the new added text.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context="com.blogspot.android_er.androidautotextview.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"/>

    <EditText
        android:id="@+id/textin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"/>

    <Button
        android:id="@+id/btnappend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Append Text"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textout1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="20dp"
            android:background="#D0D0D0"/>
        <TextView
            android:id="@+id/textout2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="20dp"
            android:gravity="bottom"
            android:background="#E0E0E0"/>
    </LinearLayout>

</LinearLayout>


package com.blogspot.android_er.androidautotextview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText editTextIn;
    TextView textViewOut1, textViewOut2;
    Button btnAppend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextIn = (EditText)findViewById(R.id.textin);
        btnAppend = (Button)findViewById(R.id.btnappend);

        textViewOut1 = (TextView)findViewById(R.id.textout1);
        textViewOut1.setText("It's normal TextView.\n");

        textViewOut2 = (TextView)findViewById(R.id.textout2);
        textViewOut2.setMovementMethod(new ScrollingMovementMethod());
        textViewOut2.setText("This TextView always auto scroll down to display bottom of text.\n");


        btnAppend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String textToAppend = editTextIn.getText().toString() + "\n";
                textViewOut1.append(textToAppend);
                textViewOut2.append(textToAppend);
                editTextIn.setText("");
            }
        });
    }
}


No comments:

Post a Comment