I'm new in android developement. I'm doing an application that will go through some process and in the process, something happens. So I want a visual console in my activity screen to see what is going on. The idea is a textview in which I insert lines and it has to be refreshed always in the last line and be able to scroll to the start.
How can I do this in code?
Thanks
If you have a simple TextView
with a fixed size, within a ScrollView
, it will behave exactly like you want. The only thing you have to do is make sure you don't overwrite the existing text each time, but append it to the end. Keep a reference to the actual content of the TextView
(a simple String
would do), and update it accordingly, then use myTextView.setText(newValue)
When you set the text to the TextView
, the view will be refreshed automatically.
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
And in your activity, when you want to add newText
to your view:
this.currentText += "\n" + newText;
final TextView myTextView = (TextView) findViewById(R.id.textView);
myTextView.setText(currentText);
And finally I would also recommend to scroll down to the last line: call fullScroll(View.FOCUS_DOWN)
on the ScrollView
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With