Do not concatenate text displayed with setText. Use resource string with placeholders
Asked Answered
A

2

10

I am a newbie in android development, I want to provide a number to setText, I am facing this problem and tried many ways to solve it.

Code is:

public class GameActivity extends Activity implements View.OnClickListener{
    int correctAnswer;
    Button buttonObjectChoice1;
    Button buttonObjectChoice2;
    Button buttonObjectChoice3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //here we initialize all our varibles
    int partA = 9;
    int partB = 2;
    correctAnswer = partA * partB;
    int wrongAnswer1 = correctAnswer - 1;
    int wrongAnswer2 = correctAnswer + 1;
    TextView textObjectPartA = (TextView)findViewById(R.id.textPartA);
    TextView textObjectPartB = (TextView)findViewById(R.id.textPartB);
    buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
    buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
    buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);
    textObjectPartA.setText("" + partA);
    textObjectPartB.setText("" + partB);
    buttonObjectChoice1.setText("" + correctAnswer);
    buttonObjectChoice2.setText("" + wrongAnswer1);
    buttonObjectChoice3.setText("" + wrongAnswer2);
    buttonObjectChoice1.setOnClickListener(this);
    buttonObjectChoice2.setOnClickListener(this);
    buttonObjectChoice3.setOnClickListener(this);

errors in last 8th line to last 4th line.

Error image

Anneal answered 31/1, 2016 at 12:4 Comment(3)
SCREENSHOT of the PROBLEM which will make everything clear of what i am facing i.sstatic.net/nVdSz.pngAnneal
You need to use strings instead of "Hardcoded text".Placative
keep up your work, this is important only if you need to translate your app.Placative
I
13

the simple solution to your problem is:

textObjectPartA.setText(String.valueOf(partA));
textObjectPartB.setText(String.valueOf(partB));
buttonObjectChoice1.setText(String.valueOf(correctAnswer));
buttonObjectChoice2.setText(String.valueOf(wrongAnswer1));
buttonObjectChoice3.setText(String.valueOf(wrongAnswer2));

what android studio is suggesting you is that if you want to append something in your textview then you have to use placeholders.

example of using placeholders is as follows:

file: strings.xml

...
<string name="part_a">part a value = %1$s.</string>
...

file: activity_main.xml

...
<TextView
    android:id="@+id/R.id.textPartA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/part_a" />
...

filename: GameActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //here we initialize all our varibles
    int partA = 9;
    int partB = 2;
    correctAnswer = partA * partB;
    int wrongAnswer1 = correctAnswer - 1;
    int wrongAnswer2 = correctAnswer + 1;
    TextView textObjectPartA = (TextView)findViewById(R.id.textPartA);
    TextView textObjectPartB = (TextView)findViewById(R.id.textPartB);
    buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
    buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
    buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);

    Resources res = getResources();
    String partA_text = String.format(res.getString(R.string.part_a), partA);
    textObjectPartA.setText(partA_text );
...

i hope this clears your doubt. Formatting strings: android developer

Industry answered 31/1, 2016 at 12:50 Comment(1)
Why do we need Resources res = getResources(); ? I have tried to run the code without it and worked, will this have any consequences?Weingartner
A
0

The warning is against concatenating (joining) strings inside the setText() method. As you are assigning integer values the best way to do this is as follows textObjectPartA.setText(String.valueOf(partA));

Anklet answered 31/1, 2016 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.