Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set text to a text view from a string.xml and normal string at a time

Tags:

android

R.string.Converasation contains Conversation Here is the code

tv.setText(R.string.Converasation+"HELLo");

textview view is taking int value of R.string.Converasation and string value of HELLo and finally displays 2131034187Hello but i want ConverasationHello

how to resolve this..

like image 842
Sando Avatar asked Aug 04 '11 09:08

Sando


People also ask

Which method is used to set the text in a text view?

java file, first of all, we access the TextView from the xml file by the line "TextView txtView = findViewById(R. id. text_view);" and in the next line we set the text by the line "txtView. setText("Android TextView");" .

What is the use of string xml?

String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.

What is a text string in xml?

Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). So, you can combine string resources with other simple resources in the one XML file, under one <resources> element. file location: res/values/filename.xml.

What is the difference between text view and plain text in Android Studio?

Textview : should be used for uneditable text which user wants to read but not to manipulate. e.g. News, Articles, Blogs. Plain text/ Edittext : should be used for taking user input in alphanumeric form. e.g. UserId, Comments.


4 Answers

Try

tv.setText(getString(R.string.Converasation) + "HELLo");

Marc.

like image 91
SnowyTracks Avatar answered Oct 12 '22 11:10

SnowyTracks


It should be like:

tv.setText(getString(R.string.Converasation)+" Hello ");

FYI, You can use either getString(int) or getText(int) to retrieve a string.

like image 23
Paresh Mayani Avatar answered Oct 12 '22 12:10

Paresh Mayani


@Mudassir everything works great. for me it's:

  display.setText(getText(R.string.counting_result) + " "+ counter);
like image 26
Robert Avatar answered Oct 12 '22 11:10

Robert


Try following code:

  Resources res = context.getResources();
  float floatConverasation = 1.1f;
  String strfloatConverasation = "HELLO";
  tv.setText(String.format(Locale.ENGLISH,"%s %f %s",
    res.getString(R.string.conversation), floatConverasation, strfloatConverasation));
like image 21
mojtaba Avatar answered Oct 12 '22 10:10

mojtaba