ono
ono

Reputation: 3102

setText not displaying value

One of my textViews is not being adjusted when I call the function:

public void wordList() {
         setContentView(R.layout.activity_main);
         TextView lv = (TextView) findViewById(R.id.listText);
         lv.setText("Text");
}

Here's the xml: If I add android:text="Text" to the xml it works.

<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" 
    android:gravity="center_horizontal"
    android:background ="#268496" >

    <LinearLayout android:id="@+id/linear"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

     <TextView
        android:id="@+id/prefixText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textIsSelectable="true"
        android:textSize="12pt"
        android:typeface="sans" />

     <EditText
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:id="@+id/input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:textSize="12pt"
        android:maxLength="1"
        android:typeface="sans" />

    </LinearLayout>

      <TextView
        android:id="@+id/listText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/linear"
        android:textColor="#FFFFFF"
        android:textIsSelectable="true"
        android:textSize="12pt"
        android:typeface="sans" />

</RelativeLayout>

Upvotes: 0

Views: 2189

Answers (3)

codeMagic
codeMagic

Reputation: 44571

If you call setContentView() after running it here then it will overwrite this call and set the content to its default (what's in the xml). If you call setContentView() say in onCreate() then you don't need to call it again as long as the TextView is inside that xml.

So, call setContentView() in onCreate() then call your function

 wordList();

then

public void wordList() {
     TextView lv = (TextView) findViewById(R.id.listText);
     lv.setText("Text");
}

Upvotes: 2

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

You're not doing anything "wrong" in your posted example. The kinds of things that may be in play here are:

a) is your TextView within the visible area (hard code some text to see if it shows up)

b) are you sure you're calling the method that sets the text?

c) do you have some other overlapping ID somewhere else that is confusing this process?

It has to be something like that.

Upvotes: 0

yyunikov
yyunikov

Reputation: 5897

You have white color of textview's text. So if the background color is white, then you just don't see it.

Upvotes: 1

Related Questions