Android setText / R.string / values
Asked Answered
H

8

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

Handy answered 18/7, 2012 at 7:28 Comment(3)
what does "still not work" means? What do you expect?Doan
It's because R.string.mycar is an int that refers to a resource. It's not the actual resource. @user1417127 's answer looks good. It fetches the actual string resource for youExtraterritorial
use context.getResources.getString(R.string.xyx).Queri
U
27

Try this

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

Ullman answered 18/7, 2012 at 7:30 Comment(0)
E
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

Enforcement answered 18/7, 2012 at 8:4 Comment(0)
A
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));
Aldosterone answered 6/9, 2016 at 10:48 Comment(1)
How to deal with the Android Studio warning: "Do no concanate text displayed with setText. Use Resource string with place holders" ?Frisbie
B
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);
Bicyclic answered 18/7, 2012 at 7:35 Comment(0)
H
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..

Historiographer answered 18/7, 2012 at 7:34 Comment(2)
instead of context can we write getActivit() ? are there any problemPeriotic
@Periotic It depends on your requirement. Read the document hereHistoriographer
S
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);
Synergistic answered 18/7, 2012 at 7:30 Comment(0)
A
0

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

Adventist answered 18/7, 2012 at 8:3 Comment(0)
S
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.

Scoff answered 17/3, 2023 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.