Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setText on a TextView will overwrite previous text

I have a text view that is by itself as part of a linear layout. The XML is:

<TextView
    android:id="@+id/label_text"
    android:layout_width="fill_parent"
    android:layout_height="77dp"
    android:layout_marginTop="3dp"/>

In the main program I first set the text view to a value:

private TextView character_speaking;

protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    character_speaking = (TextView) findViewById(R.id.label_text);
    character_speaking.setText("a text string that was large enough for two lines");
}

This works well.

Then I do another setText in response to a button push:

public void handleNextButtonClick(View view) {
    character_speaking.setText("first one line message");
}

And this worked as well

But then I did another setText in response to another button push:

public void handlePrevButtonClick(View view) {
    character_speaking.setText("second one line message");
}

And disaster strikes. The second one line message writes over the first one line message without clearing it.

Is there anyway to clear the first one line message before writing the second one line message?

I have already tried to get around this problem by using an EditText instead of a TextEdit but this was not acceptable because it allowed the user to write in the field.

like image 883
Craig Roll Avatar asked Nov 11 '22 11:11

Craig Roll


1 Answers

I've encountered this similar issue. What made it work is funny -> I just put a black background color to my layout (LinearLayout) and it is working fine now.

It seems the background is not cleared, if it doesn't know what color to clear it with.

like image 159
ni_lus Avatar answered Nov 14 '22 22:11

ni_lus