Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a working example I got running in the emulator and on my galaxy s. Basically there are two buttons to add text to the bottom text view, depending on your devices size only the second of these should be usable to see the autoscrolling. The textview being edited uses the ontextchangedlistener to check the scroll position before its text is changed and then to call a delayed (so the screen can update with the additional text) autoscroll where appropriate, after the text has changed.</p> <p>The layout xml is as follows:</p> <pre><code> &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/scrollmain"&gt; &lt;RelativeLayout android:id="@+id/mainRelative" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;TextView android:id="@+id/titleText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the scrolling test application!" /&gt; &lt;Button android:id="@+id/firstTextAddButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/titleText" android:text="Click me to add text to the textview below without scrolling"/&gt; &lt;Button android:id="@+id/secondTextAddButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/firstTextAddButton" android:layout_marginTop="380dp" android:text="Click me to add text to the textview below and scroll (if you are currently scrolled all the way to the bottom)"/&gt; &lt;TextView android:id="@+id/textToEdit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/secondTextAddButton" android:textSize="18sp" android:text="Some text to get us started."/&gt; /&gt; &lt;/RelativeLayout&gt; &lt;/ScrollView&gt; </code></pre> <p>And the code for the activity is as follows:</p> <pre><code> package code.example.scrollingontextchange; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.ScrollView; import android.widget.TextView; public class MainActivity extends Activity { private int scroll_pos; private int maxScrollPosition; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView)findViewById(R.id.textToEdit); tv.addTextChangedListener(new TextWatcher(){ @Override public void afterTextChanged(Editable s) { if(scroll_pos == maxScrollPosition) { Handler h = new android.os.Handler() { @Override public void handleMessage(android.os.Message msg) { switch(msg.what) { case 0 : try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ScrollView scrll = (ScrollView)findViewById(R.id.scrollmain); scrll.fullScroll(ScrollView.FOCUS_DOWN); break; default : break; } } }; h.sendEmptyMessage(0); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { ScrollView scrll = (ScrollView)findViewById(R.id.scrollmain); RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainRelative); scroll_pos = scrll.getScrollY(); maxScrollPosition = rl.getHeight() - scrll.getHeight(); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } }); OnClickListener addTextOnClick = new OnClickListener() { @Override public void onClick(View v) { TextView tv = (TextView)findViewById(R.id.textToEdit); tv.setText(tv.getText() + "\nA long time ago, in a galaxy s far far away............"); } }; Button b = (Button)findViewById(R.id.firstTextAddButton); b.setOnClickListener(addTextOnClick); Button b2 = (Button)findViewById(R.id.secondTextAddButton); b2.setOnClickListener(addTextOnClick); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } </code></pre> <p>Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload