49

How can I display an Integer value in TextView?

When I try, I get an error android.content.res.Resources$NotFoundException: String resource ID

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Maya
  • 535
  • 1
  • 6
  • 8
  • 16
    Six answers, and noone mentions why it doesn't "just work" when you specify an integer value in the setText() method. After a bit looking around I've concluded that the problem is that setText() with an integer value assumes that the integer is actually a resource ID number. – RenniePet Jun 01 '15 at 00:36
  • 1
    and thats why the app crashes with a weird error `UncaughtException: android.content.res.Resources$NotFoundException` – Faisal Naseer Feb 24 '17 at 10:45

9 Answers9

86
TextView tv = new TextView(this);
tv.setText(String.valueOf(number));

or

tv.setText(""+number);
Muhammad Abdullah
  • 2,474
  • 2
  • 19
  • 17
47

TextView tv = new TextView(this);
tv.setText("" + 4);
Falmarri
  • 47,727
  • 41
  • 151
  • 191
  • 4
    @BoldijarPaul, `setText(String.valueOf(variable));` sample code provided in [Muhammad Abdullah's answer](https://stackoverflow.com/a/3994387/6398434) doesn't and I can confirm it works pretty well. – JorgeAmVF Apr 18 '18 at 15:21
  • 1
    @JorgeAmVF both work good, but the 1st one will give you a lint warning. – Boldijar Paul Apr 19 '18 at 06:13
16

If you want it to display on your layout you should

For example:

activity_layout.XML file

    <TextView
        android:id="@+id/example_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

On the activity.java file

    final TextView textView = findViewById(R.id.example_tv);
    textView.setText(Integer.toString(yourNumberHere));

In the first line on the XML file you can see:

android:id="@+id/example_tv"

That's where you get the id to change in the .java file for the findViewById(R.id.example_tv)

Hope I made myself clear, I just went with this explanation because a lot of times people seem to know the ".setText()" method, they just can't change the text in the "UI".


EDIT Since this is a fairly old answer, and Kotlin is the preferred language for Android development, here's its counterpart.

With the same XML layout:

You can either use the findViewbyId() or use Kotlin synthetic properties:

findViewById<TextView>(R.id.example_tv).text = yourNumberHere.toString()

// Or

example_tv?.text = yourNumberHere.toString()

The toString() is from Kotlin's Any object (comparable to the Java Object):

The root of the Kotlin class hierarchy. Every Kotlin class has Any as a superclass.

Joaquim Ley
  • 4,038
  • 2
  • 24
  • 42
15

Consider using String#format with proper format specifications (%d or %f) instead.

int value = 10;

textView.setText(String.format("%d",value));

This will handle fraction separator and locale specific digits properly

Alec Sanger
  • 4,442
  • 1
  • 33
  • 53
Rakesh Barik
  • 189
  • 1
  • 5
7

Alternative approach:

 TextView tv = new TextView(this);
 tv.setText(Integer.toString(integer));
IgorK
  • 886
  • 9
  • 26
John
  • 15,418
  • 12
  • 44
  • 65
3
String s = Integer.toString(10);

Then setText(s)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
VK Golakiya
  • 79
  • 1
  • 7
  • This is just a repeat of [this existing answer](https://stackoverflow.com/a/4080552). – Pang Sep 11 '18 at 07:56
1

just found an advance and most currently used method to set string in textView

textView.setText(String.valueOf(YourIntegerNumber));

Azhar
  • 109
  • 1
  • 5
0

It might be cleaner for some people to work with an Integer object so you can just call toString() directly on the object:

Integer length = my_text_view.getText().getLength();
counter_text_view.setText( length.toString() );
Kacy
  • 3,330
  • 4
  • 29
  • 57
-1
tv.setText(Integer.toString(intValue))
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Yash Bhardwaj
  • 57
  • 2
  • 6
  • ʍѳђઽ૯ท What was the issue with this answer, you wrote the same answer. It's the difference in writing or its is a way to reduce points – Yash Bhardwaj Sep 21 '18 at 08:01