Horizontal scrollview of a Textview automatically slide when inserting dynamic texts

Asked

Viewed 933 times

6

I have a layout simple according to the shown below that it has a ScrollView horizontal when exceeding the limit of TextView.

But I would like when inserting text the focus of TextView was always at the last inserted value and did not need to scroll the bar to see the last inserted value.

Like an automatic scrolling to keep up with the entered values.

And return manually to the start bar if you need to.

Does anyone have any idea how to do ?

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>

1 answer

2

Try to do so:

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

sc.post(new Runnable() { 
    public void run() { 
        sc.fullScroll(ScrollView.FOCUS_DOWN); 
    } 
}); 
  • this object "scroller" you created where ?

  • opa, is the Scrollview itself. I simplified the in the statement and forgot there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.