Question

I have a one textView in a layout that is dedicated to be header line of a list.

the layout - list_header.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:text="@string/list_header"
    android:textSize="22sp"
    android:textStyle="bold" >
</TextView>

and I call it from main.java (that display a list):

View header = (View)getLayoutInflater().inflate(R.layout.list_header, null);
mList.addHeaderView(header);

I want to change the text attribute of the layout via code before display. How do I do that ?

Was it helpful?

Solution

You'll need to add an ID to your XML code like so:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textToChange
    android:gravity="center_vertical"

    <!-- Remove this line because you won't need it -->
    android:text="@string/list_header"

    android:textSize="22sp"
    android:textStyle="bold" >
</TextView>

Now in your main.java file, find the text view and set it's text like so:

//Declare this as a private variable:
private TextView textView1;

//Then, wherever your header declared, do this:
textView1 = (TextView) findViewById(R.id.textToChange);
textView1.setText("Your desired text");

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top