Puni
Puni

Reputation: 1294

Android binding double value to view using string resources

Trying to bind double value to view using string resources.

Attempt 1:

layout.xml

android:text="@{@string/user_popularity(Double.toString(user.popularity))}"

string.xml

<string name="user_popularity">Popularity: %s</string>

Attempt 2:

layout.xml

android:text="@{String.format(@string/user_popularity, user.popularity)}"

string.xml

<string name="user_popularity">Popularity: %.1f</string>

Error:

Error:(167, 107) error: double cannot be dereferenced

Here are some similar questions

  1. How do I format a double using Android view data bindings?

  2. How do I use databinding to combine a string from resources with a dynamic variable in XML?

Upvotes: 2

Views: 1744

Answers (1)

Puni
Puni

Reputation: 1294

Actually it was quite simple, my bad I was doing the wrong way. Here's the solution.

<string name="user_popularity">Popularity: %.1f</string>
android:text="@{@string/user_popularity(user.popularity)}"

Upvotes: 4

Related Questions