It is possible to last a variable in Strings.xml

Asked

Viewed 791 times

0

I’m trying to get a string from the.xml string to replace a field on the screen, but I’ve been searching and I’m not being able to use it as a variable

xml string.

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

java class.

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

xml layout.

<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" />

Upshot:

inserir a descrição da imagem aqui

1 answer

2


I don’t know if that’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);

Taking its string as an example, this has no usefulness.

An example where it can be useful is in 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 initial part is chosen automatically, according to the language, and then the name.

  • Oh yes, so there’s no way to insert the data into the.xml string by the java class ?

  • Not that I know of.

  • Is that actually I tried to insert right into the field I show in the image, but it did not work

  • For when trying to insert the text, in that Textview the application 'crashava'

  • Right, so I would replace my xml with this one and it would supposedly trade?

  • I believe that your difficulty has nothing to do with your question. If what you want to know is how to change that Textview via java it is better to ask another question (not to invalidate this one) like: How to change Navigationview Headerview text?

  • Oh yes thank you, I was trying the way I asked the question, but you said it wouldn’t work

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.