I have a TextView with a string where I need to insert data.
Something like: "some text %d other text".
I don't want to have a hardcoded text in my sources.
Are there any good practices for that?
5 Answers
If you want to append some data to existing data of TextView then use this:
String finalText = (textView.getText().toString()).concat(" and new text here");
textView.setText(finalText);
Hope this helps.
EDIT:
Hardcoded text was an example:
You can simply replace it by String.
String someText; //This can be retrieved from webservic or by any means you want.
String finalText = (textView.getText().toString()).concat(someText);
textView.setText(finalText);
1 Comment
ilia
I've mentioned that I don't want to have a hardcoded text in my code.
If you want to format text with pattern like %d %s, you can use it as :
String yourText = "default text"; // can be assigned runtime
Integer number= 2;
textView.setText(String.format("your data %s %d", yourText, number));
You can use any String variable here which holds your desired value.
Hope it helps ツ
String text ="whatever it is"; textView.setText(text);