<ScrollView
android:id="@+id/readScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/readTextView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
this is sample XML file.
ScrollView readScroll = findViewById(R.id.readScrollView);
TextView readTextView = findViewById(R.id.readTextView);
String largeText = So Large Text; // up to megabytes.
readTextView.setText(largeText); // So large that it spend some time.
Log.e("amount", readScrollView.getMaxScrollAmount()); // This Code return 0
My problem is Log return 0.
I think this is because of Log was executed before setText()
end this work. So I tried to use thread and use Thread.join()
, it doesn't work.
I already checked "Is getMaxScrollAmount()
method run properly?" with onClickListener, This method run properly and return 1280.
How to run Log.e("amount", readScrollView.getMaxScrollAmount());
this code after completely end setText Code??
Try this code:
mScrollView = findViewById(R.id.content_scroll);
mContentTxt = findViewById(R.id.content_txt);
mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// MaxScrollAmount return non-zero
int maxScrollAmount = mScrollView.getMaxScrollAmount();
}
});
mContentTxt.setText("Long string here");
Basically, you listen to the ScrollViewer
layout changed, which is called after its child views do something to its size. Register the event right before setText
and unregister on its first called.