SingleWave Games
SingleWave Games

Reputation: 2648

Android - Creating a dynamic TextView

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_width="fill_parent"
    android:layout_height="135dp"
    android:gravity="center_horizontal"
    android:layout_below="@+id/spacer1" >

    <TextView
            android:id="@+id/battle_details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Console with information" />
 </RelativeLayout>

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

Upvotes: 0

Views: 667

Answers (1)

toadzky
toadzky

Reputation: 3846

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

Upvotes: 3

Related Questions