10

I am having trouble with setting text in a text view with format and multiple values.

holder.car.setText(R.string.mycar + lm.getCarName() + R.string.year + lm.getYear());

this is giving me " 2143545 Camero 2143213 1977 "

I have tried few other "solutions" from the web

holder.car.setText(getString(R.string.mycar) + lm.getCarName() + getString(R.string.year) + lm.getYear());  << not work, getString undefine>>

I even tried String.valueOf(R.string.mycar); getResources().getText(R.String.mycar), still it didn't work.

It would be great if someone can help me, thanks

Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
S Arumik
  • 633
  • 2
  • 10
  • 21

8 Answers8

26

Try this

holder.car.setText(getResources().getString(R.string.mycar));

Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
user1417127
  • 1,525
  • 1
  • 21
  • 29
14

I think you're trying to use parameters in your string.

Try this:

<string name="mycar">Car: %1$s Year: %2$s</string>

String mycar = getString(R.string.mycar);
mycar = String.format(mycar, lm.getCarName(), lm.getYear());

You should get:

Car: Camaro Year: 1977

Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
6

If you want to set your textview just a string from your string.xml file,

mytextview.setText(R.String.mycar);

If you want to set your textview with combination of some strings or integers, (better than first way)

int number=5;

mytextview.setText(getResources().getString(R.String.mycar) + " " +number + " " + getResources().getString(R.String.mysecondcar));
  • How to deal with the Android Studio warning: "Do no concanate text displayed with setText. Use Resource string with place holders" ? – mparkes Sep 09 '18 at 04:39
4

R.string.mycar and R.string.year are only IDs for resources. For this reason you get the numbers (IDs are numeric).

To get string from resources you need to use this construction:

String myCar = getResources().getString(R.string.mycar);

and now the myCar variable holds the string you put in strings.xml file under the mycar name.

the method getResources() belongs to Context. If you run your code outside an Activity, use the context instance to get the string, like this:

String myCar = context.getResources().getString(R.string.mycar);
Zelter Ady
  • 6,266
  • 12
  • 48
  • 75
3

Try this. If you're fetching string in class without extending Activity Get using your Context

holder.car.setText(context.getResources().getString(R.string.mycar));

If you're extending Activity

holder.car.setText(yourActivity.this.getResources().getString(R.string.mycar));

Hope this helps you..

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • instead of `context` can we write `getActivit()` ? are there any problem – mehmet May 08 '14 at 15:11
  • @mehmet It depends on your requirement. Read the [document here](http://developer.android.com/reference/android/app/Fragment.html#getActivity()) – Praveenkumar May 09 '14 at 04:45
2

You have to retrieve your resources first, and the call the medthod getString(int), not getText, has you have put.

So, it should be:

getResources().getString(R.String.mycar);
Iñigo
  • 12,723
  • 2
  • 31
  • 31
0

The R class contains kind of pointers to your ressources, so you can not directly use them, use getResources().getString(), as others say.

Rocel
  • 1,029
  • 1
  • 7
  • 22
0

You have to use

context.getText(R.string.mycar);

just.
The context is the activity that is passed to the adapter.
It works for me.