Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android setText / R.string / values

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

like image 759
S Arumik Avatar asked Jul 18 '12 07:07

S Arumik


3 Answers

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));
like image 141
Gökhan Sarıkaya Avatar answered Sep 28 '22 02:09

Gökhan Sarıkaya


Try this

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

like image 26
user1417127 Avatar answered Oct 18 '22 01:10

user1417127


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

like image 14
Benito Bertoli Avatar answered Oct 18 '22 02:10

Benito Bertoli