android – The horizontal ScrollView of a TextView will automatically slide when inserting dynamic texts

Question:

I have a simple layout as shown below which has a horizontal ScrollView when going over the TextView boundary.

But I would like that when inserting text the focus of the TextView would always be on the last value inserted and not need to scroll the bar to see the last value inserted.

Like an automatic scroll to follow the values ​​entered.

And go back with the bar manually to the beginning if you need to.

Does anyone have an idea how to do it?

Activity :

public class MainActivity extends AppCompatActivity {
  @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);       
     textview = (TextView) findViewById(R.id.textview);
     textview.setMovementMethod(new ScrollingMovementMethod());
   }
}

xml:

    <HorizontalScrollView
      android:id="@+id/horizontalScrollView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="right">

      <TextView
           android:id="@+id/display0"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:editable="false"
           android:gravity="center|right" />
      </HorizontalScrollView>
</LinearLayout>

Answer:

Try doing like this:

ScrollView sc = ((ScrollView) R.findViewById(R.id.scroll);

sc.post(new Runnable() { 
    public void run() { 
        sc.fullScroll(ScrollView.FOCUS_DOWN); 
    } 
}); 
Scroll to Top