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.



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">

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"/>

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

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

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/textout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20dp"
android:background="#D0D0D0"/>
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"/>





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("");
}
});
}
}


Popular posts from this blog

OnePlus Releases OxygenOS 4.5 OTA For OnePlus 3 and OnePlus 3T

Report: Incredibly Accurate GPS Chips are Coming to Smartphones Next Year

Black Friday saw the lowest price yet for the Roomba j7 from iRobot.