Home > Mobile >  how to call Strings in mainactivity.xml from Strings.xml file
how to call Strings in mainactivity.xml from Strings.xml file

Time:07-16

  1. I want to Call it in TextView

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

CodePudding user response:

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

CodePudding user response:

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

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    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.

  • Related