Saturday, October 29, 2011

How to change the text in a TextView

First off, you need a TextView defined in your XML file.  The one that I am going to be using is called status. Below is the relevant portion of the XML code.

 

<TextView
    android:id="@+id/status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

below is the code that I used to change the text.

 

        // Update the status field.           
        TextView text = (TextView)findViewById(R.id.status);
        text.setText("Button1 clicked");

In this example, I declared a variable text of type TextView.  I then used the setText method to define the text that should appear in the TextView.

No comments:

Post a Comment