Take the 2-minute tour ×

I am creating an Android game and have an issue with dynamic text with a TextView. Essentiall within my layout, I have a TextView, within a Relative Layout with enough space for several lines.

What I would like to do is add 5 lines within the TextView, with a functionality that once it is trying to write a 6th line it automatically overwrites line 1, therefore only ever showing a max of 5 lines of text.

Example of What I am after:

dynamic line 1
dynamic line 5

Please find below my xml code:

<RelativeLayout android:id="@+id/battleconsole" 
    android:layout_below="@+id/spacer1" >

            android:text="Console with information" />

This is the code that I am using to update the text within the TextView:

TextView update = (TextView)findViewById(R.id.battle_details);
update.setText("test console data going in here");

I am not sure if this is even possible using a TextView, if not is there any other way I can solve this issue?

Thanks in advance

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Keep the 5 lines in a collection (array or list). When you set the text on the TextView, join the lines in the collection on "\n". Then just replace the item in the collection with the updated text.

Android has a join method in the TextUtils class, but I prefer the Guava library's Joiner class. Check it out: Google Guava

share|improve this answer
So simple and such an elegant way of doing things ;-) –  Darwind Oct 16 '12 at 17:12
Works a treat, Thank you. –  LandL Partners Oct 16 '12 at 22:27

Your Answer


