1
  1. I want to Call it in TextView

    <string name="Std_Name">Name:       Muhammad Abdullah</string>
    <string name="Std_Phone">Phone:     +923338157028</string>
    

4 Answers 4

2

If you want to add the value from strings.xml to TextView in mainactivity.xml , add the following line of code to your xml

android:text="@string/Std_Name"

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="5sp"
    android:lineSpacingExtra="2sp"
    android:text="@string/Std_Name" // Add this line to your textView
    android:textColor="@color/yellow_dark"
    android:textSize="@dimen/activity_text_size_small"
    android:textStyle="normal" />

Or, if you want to add text from strings.xml to textview from MainActivity.java, add the following code

TextView textView = findViewById(R.id.textView1);
textView.setText(getResources().getString(R.string.Std_Name));
Sign up to request clarification or add additional context in comments.

Comments

1

From within an XML layout, you can use the @string/resourceName syntax within the text string to reference a string resource:

android:text="@string/resourceName"

Using the Std_Name string resource from your example, that would look something like this:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/Std_Name" />

Official documentation for string resources can be found here. You can also find a good example in the Accessing resources from XML section of the App resources overview.

Comments

0

Try this

String myName = getResources().getString(R.string.Std_Name);
String myPhone = getResources().getString(R.string.Std_Phone);

// Then do whatever you want with these values

2 Comments

how to use them in MainActivity.XML
first of all, pls explain how you want to use the values from strings.xml.
0

Call in your Textview text field

android:text="@string/Std_Name"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.