Apps development for Android step by step

Lesson 1.3: How to modify TextView in Java code (findViewById, setText and getText methods)

We have already presented some text on the screen using layout XML file. This is recommended approach, but if we want to modify that text we have to use Java.

If you are new to Java read a few lessons called Java basics that we have already published (they are a part of Lesson 0 set). We’re going to use concepts like classes, methods and variables explained there.

In the lesson 1.1 we were using TextView tag. TextView is also Android class for displaying text. So in Java code we would use it too, but instead of attributes we will modify it by methods.

There are three scenarios:

  1. Modifying a text added in layout XML file.
  2. Adding a TextView directly in Java instead of layout XML file.
  3. Adding additional TextView elements beside that from layout XML file.

Now we focus only on scenario 1, leaving 2 & 3 for future lessons.

We finished previous lesson displaying I’m the best from strings.xml file. But imagine that we would like to change this text to I love you.  Of course to make such an amazing Valentine’s app we could just modify a content of strings.xml file, but let’s assume that firstly we want to show I’m the best and later change it to a new string.

To achieve this goal we have to accomplish two tasks: find TextView in layout file and then change a text it displays.

Step 1. Adding ID to TextView

The easiest way to find element of the Android app layout is to use its ID. We could add our own IDs to almost every XML tags. To add ID we use android:id attribute.

This is an example:

The android:id syntax looks very similar to android:text (what we were practicing in the previous lesson). As you remember @ means that we refer to resources, then we give resource type and after slash resource name (@resource_type/resource_name). In case of ID we have additional plus sign (@+id/id_name). This is because we don’t have any resource file with list of all IDs. Instead IDs are added (that’s why we use +) or generated when the app is started.

Open activity_main.xml file and add android:id attribute to TextView using above example.

Step 2. Finding TextView using ID (and importing a TextView class).

We could close activity_main.xml file and switch to file. We would add some commands to onCreate method (you could learn more about onCreate from this lesson), so they would be executed just after starting an app.

To find TextView we will use findViewById() method (it allows to find any type of View, but now we know only TextView…). First we have to create a variable based on TextView class. We name it textElement, but of course you could choose your name.

Put this line after: setContentView(R.layout.activity_main). This line displays on device screen layout resource – in this case activity_main.xml file.

By the way maybe you have noticed that adding this code also resulted in a new line in import section at the top of file:

Import is used to add predefined Android or Java classes from other packages, but we don’t have to bother about it as Android Studio adds import lines automatically as we start to use some classes.

Let’s go back to TextView. Now we have to assign TextView from layout file to TextView variable.

We have to set in brackets a type of View we’re looking for and then give type and name of resources:  (view_type) findViewById(

Instead of using two lines for TextView declaration and assigning TextView tag, we could combine them.

This is a full code of onCreate:


Step 3. Replacing a text via setText()

As we have now access to TextView all we have to do it is just use setText() method with a new text as argument .

Mind that this would completely replace an original text. Of course instead of using the text directly into a code we rather should refer to string resources as explained in previous lesson.

First add a new definition to strings.xml file:

And now refer to it:

Below you find full code of onCreate method (mind that there are two lines with setText() doing the same, but in different way) and strings.xml file.


We’ve found TextView in layout XML file and set a new text to it (Android Studio)

We’ve found TextView in layout XML file and set a new text to it (Android Studio)

There is one more option. As an argument of setText() we could also use a String variable (so the variable storing a text).

First define a variable (for instance named newText), then assign some text to it and finally set this text to TextView.

Instead of assigning a text we could again refer to string resources. For this we need two methods – getResources()referring to resources pool and getString() referring to string resources with string name as argument .

We’ve used setText with String variable. Tricky part was referring to String resources via getString (Android Studio)

We’ve used setText with String variable. Tricky part was referring to String resources via getString (Android Studio)

Step 4. Getting a text via getText() and extending it

In the previous step we replaced one text with another. There is also possibility to read original text and modify it.

First we have to create a variable that would store original text from layout file. Because it’s for text the type should be String. Let’s call it modifyText.

Now we have to assign original text to it. To read the text we use getText() method on the specific TextView instance. Because getText() returns so called CharSequence and we need String, we additionally use toString() that coverts it.

Now we could modify the value of variable. We do the simplest modification – just add extra text. Later we will practice much more sophisticated String operations.

And as previously we set this text to TextView.

This time we extended text we had previously get from TextView (Android Studio)

This time we extended text we had previously get from TextView (Android Studio)

Step 5. Expanding text in TextView by append() method

The same what we did in previous step we could do just by one method. If we want to add something to a text displayed by TextView, we don’t need to get text first, modify it and set that text again. Just use append() instead. Of course we still need to refer to TextView element found via findViewById().

This is a code:

Instead of a specific string you could use a String variable or string resource as in the previous step. Mind that additional text would be glued exactly at the end of a current one, so usually it’s necessary to add a space at tge beggining, like ” More text”.

Step 6. Preview only in emulator

If we now switch again to main_activty.xml layot file and open a preview window (use Preview tab on the right edge of screen), we will still see I’m the best text. Preview works only for layout in XML files. Java code isn’t executed until you run an app. So too see an effect you have to start an emulator (read more) or test an app on real devices (read more).

Summary: We could easily modify a text added in layout file. First we have to find right TextView tag using its ID and then use method setText() to modify a text. Method setText() as an argument could use specific text, string resource or String variable. We’ve also managed to use getText() to read a text assigned to specific TextView.