2

I have a TextView whose contents are copied from a text file. Now each time the contents of the text file is loaded into the TextView, I want it to scroll down automatically to the end. This is what that portion of my layout XML file has :

    <ScrollView
        android:id="@+id/scroller"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/command"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/header"
        android:fillViewport="true" >

        <TextView
            android:id="@+id/output"
            android:layout_width="fill_parent"
            android:layout_height="132dp"
            android:bufferType="spannable"
            android:editable="false"
            android:enabled="false"
            android:focusable="true"
            android:focusableInTouchMode="false"
            android:freezesText="true"
            android:inputType="textMultiLine"
            android:isScrollContainer="true"
            android:scrollbars="vertical"
            android:text="@string/output" >

            <requestFocus />
        </TextView>
    </ScrollView>

And this is what the function looks like :

public void displayOutput()
{
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"/Android/data/terminalemulatorlog.txt");
    StringBuilder text = new StringBuilder();
    try 
    {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) 
        {
            text.append(line);
            text.append('\n');
        }
    }
    catch (IOException e) 
    {
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
    }
    TextView output=(TextView) findViewById(R.id.output);
    output.setText(text);
    ((ScrollView) findViewById(R.id.scroller)).post(new Runnable() 
    {
        public void run() 
        {
            ((ScrollView) findViewById(R.id.scroller)).fullScroll(View.FOCUS_DOWN);
        }
    });
}

Now I found a partial solution over here. Hence the last line bit of code that says :

((ScrollView) findViewById(R.id.scroller)).post(new Runnable() 
{
    public void run() 
    {
        ((ScrollView) findViewById(R.id.scroller)).fullScroll(View.FOCUS_DOWN);
    }
});

But this works only the first time the text file is loaded. How do I always make the TextView scroll down to the end?

Community
  • 1
  • 1
Vishnu
  • 2,024
  • 3
  • 28
  • 34
  • You can automatically scroll down to the bottom 1) when the text is loaded, 2) when the activity is first displayed (onCreate()) or 3) every time the activity is displayed (onPrepare()). Q: Is that what you're looking for? – paulsm4 Dec 23 '11 at 20:18
  • The first point. I want it to automatically scroll down to the bottom when the text is loaded. onCreate() doesn't do this. And I'm not sure of what onPrepare() does. – Vishnu Dec 24 '11 at 05:52

2 Answers2

9

Just add to your TextView:
android:gravity="bottom" android:scrollbars = "vertical"

and set movement method:
myTextView.setMovementMethod(new ScrollingMovementMethod());

Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
Apelt
  • 91
  • 1
  • 1
  • 2
    Welcome to SO! Just as an FYI, surrounding text with backticks (`) will format single lines of code. And if you want a new line, you can put a 2 spaces after the previous line. – Broots Waymb Jan 09 '19 at 14:15
7

EDIT: Use this:

    final ScrollView scroller = (ScrollView) findViewById(R.id.scroller);
    scroller.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                scroller.fullScroll(View.FOCUS_DOWN);
            }
        }
    });
Noureddine AMRI
  • 2,942
  • 1
  • 21
  • 28
  • When I use the following piece of code, the app crashes :( ScrollView output=(ScrollView)findViewById(R.id.output); output.setOnFocusChangeListener(scrolldown); – Vishnu Dec 24 '11 at 05:45
  • The output scrolls down, but not completely. I've got an EditText box right below this. Could that be the problem as the Keypad is over the TextView almost always? I want this to be like the SMS Apps, where regardless of the status (maximised/hidden) of the Keyboard, it scrolls down each time a new SMS is received. – Vishnu Dec 26 '11 at 15:24
  • 3
    Down-voted due to lack of proper description. It is just a chunk of code. – Octavian Helm Jan 02 '12 at 20:12