5

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Craig Roll
  • 106
  • 1
  • 5
  • Have you tried using character_speaking.setText(""); before you try to write the second message? – ElectronicGeek Mar 12 '14 at 19:55
  • 5
    setText() should be clearing the previous message. Can you post a screenshot of what's going on? Also, if you want to use an EditText, you can set `android:inputType="none"` which will disable editing. – BVB Mar 12 '14 at 19:57
  • 1
    It's not clear to me what's wrong, is the text overlapping? – ElectronicGeek Mar 12 '14 at 19:59
  • 2
    Screenshot would be nice. – Embattled Swag Mar 12 '14 at 20:01
  • I just tried character_speaking.setText(""); before the second message and there was no change in behavior. The second message was written on top of the first one. I also have tried for EditText android:inputType="none" and this did not work because the user can still touch the EditText and have a keyboard pop up. – Craig Roll Mar 12 '14 at 20:28
  • The truly mysterious thing to me is that the first text message is cleared but not the second one. Also, things that don't work: padding the third message with blanks to be more than one line, or making all of the messages more than one line. – Craig Roll Mar 12 '14 at 20:56

6 Answers6

2

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.

ni_lus
  • 21
  • 2
0

Hope this points you in the right direction.

Updated - Working Code & Description

Application min sdk is 11, and target sdk is 19.

My Previous Answer was a little miss leading because in fact you cannot define a ViewGroup in your XML. So you have to create a basic LinearLayout with a resource Id. Here is the following XML which I have come up with for you.

I hope this is exactly what you were looking for I forgot to note for you that you can't define a ViewGroup in XML, but you have to cast your layout to a ViewGroup.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/textViewHolder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="vertical" >
</LinearLayout>

<LinearLayout
    android:id="@+id/buttonBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" 
    style="?android:attr/buttonBarStyle">

    <Button
        android:id="@+id/buttonPrevious"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="@string/text_Previous"
        style="?android:attr/buttonBarButtonStyle" />

    <Button
        android:id="@+id/buttonNext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="@string/text_Next"
        style="?android:attr/buttonBarButtonStyle"/>
</LinearLayout>

My strings.xml:

<?xml version="1.0" encoding="utf-8"?>
 <resources>
  <string name="app_name">TextViewSwitcher</string>
  <string name="text_Previous">Previous</string>
  <string name="text_Next">Next</string>
 </resources>

And Finally my MainActivity.class:

public class MainActivity extends Activity {

// ------------------------------------
// Member Variables

private LinearLayout m_textViewHolder = null;
private Button m_btnPrevious = null;
private Button m_btnNext = null;
private final String m_nothingText = "Nothing Pressed";
private final String m_previousText = "Previous Pressed";
private final String m_nextText = "Next Pressed";


// ------------------------------------
// Class Overrides

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

    // Get a reference to the UI Elements
    m_textViewHolder    = (LinearLayout) findViewById(R.id.textViewHolder);
    m_btnPrevious       = (Button) findViewById(R.id.buttonPrevious);
    m_btnNext           = (Button) findViewById(R.id.buttonNext);

    // Add Listeners to the Buttons
    m_btnPrevious.setOnClickListener(PreviousListener);
    m_btnNext.setOnClickListener(NextListener);

    // Populate the Initial Layout with a new TextView, & set the Text
    TextView nothingTextView = new TextView(getBaseContext());
    nothingTextView.setText(m_nothingText);
    ((ViewGroup) m_textViewHolder).addView(nothingTextView);

}

// -----------------------------------------
// Private Methods

// Creates  a new TextView based on the Text Passed in, and returns the new TextView
private TextView createNewTextView(String text)
{
    TextView newTextView = new TextView(getBaseContext());
    newTextView.setText(text);

    return newTextView;
}

// Removes the Child Views of the Parent ViewGroup
private void removeChildViewsFromParent()
{
    ((ViewGroup) m_textViewHolder).removeAllViews();
}

// -------------------------------------
// Listeners

private OnClickListener PreviousListener = new OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
        removeChildViewsFromParent();
        ((ViewGroup) m_textViewHolder).addView(createNewTextView(m_previousText));

    }

};

private OnClickListener NextListener = new OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
        removeChildViewsFromParent();
        ((ViewGroup) m_textViewHolder).addView(createNewTextView(m_nextText));

    }

};

}

Sorry for the Quick Answer before but this produces the following results when the buttons are pressed.

Next Button Pressed Previous Button Pressed

kandroidj
  • 13,784
  • 5
  • 64
  • 76
0

I think that I have decided on a workaround.

Instead of using a TextEdit (which apparently cannot be used for what I want), I will use an EditText.

<EditText
    android:id="@+id/label_text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:hint=""
    android:focusable="false"/>

The last line is the key. This prevents the user from clicking on the EditText and defacing the text with a keyboard.

The rest of the code is what you would expect:

private EditText character_speaking;

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

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

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

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

and everything is working fine.

Craig Roll
  • 106
  • 1
  • 5
0

You wanted to clear previous text, right? So you can use an EditText and clear the view with editText.getText().clear().

For not making it clickable just use editText.setFocusable(false) or editText.setClickable(false). I'd rather use setClickable, but try yourself.

larsaars
  • 2,065
  • 3
  • 21
  • 32
0

I was having the same problem, and it was caused by the way I set up the fragment. I had two framents running at the same time in the same in the layout.

After I removed the extra fragment, the issue resolved.

yzernik
  • 1,161
  • 2
  • 13
  • 19
-1

The TextView.setText(String) method will always overwrite the String already stored.

See the documentation for this method here: http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)

If you want to add the previous string and the new string, use:

TextView.setText(TextView.getText().toString() + String)

ElectronicGeek
  • 3,312
  • 2
  • 23
  • 35