It is possible to use a variable in Strings.xml

0

I'm trying to make a string in the string.xml replace a field on the screen, but I've been searching and I'm not able to use it as a variable

string.xml

<string name="nome">%1$s</string>

class.java

Resources res = getResources();
        String text = String.format(res.getString(R.string.nome), username);

layout.xml

<TextView
        android:id="@+id/nome1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/nome"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

Result:

    
asked by anonymous 14.07.2017 / 21:32

1 answer

2

I do not know if it's exactly what you're thinking, but you can do something like this:

Resources res = getResources();
String text = res.getString(R.string.nome, username);
textView.setText(text);

Having your string example, this is of no use.

An example that might be useful is the use of strings per language:

<string name="nome">My name is %1$s</string>

and

<string name="nome">O meu nome é %1$s</string>

The starting part is chosen automatically according to the language, and then the name is entered.

    
14.07.2017 / 21:41