2

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?

Puni
  • 1,214
  • 2
  • 18
  • 34
  • 1
    Did you have difficulty when you followed the answers? They look like they answer your question and your attempts don't use them. – George Mount Aug 02 '17 at 22:00
  • Yes, I have tried both the solution from the mention links. – Puni Aug 03 '17 at 03:51
  • This should work: `android:text="@{@string/user_popularity(user.popularity)}"` and the string resource `Popularity: %1$f` assuming that user.popularity is a `double` or `float`. What are you seeing? Is it compile-time error, a runtime exception, or is the formatting wrong? It could be that you haven't assigned a user using `binding.setUser()`. – George Mount Aug 03 '17 at 18:20

1 Answers1

4

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)}"
Puni
  • 1,214
  • 2
  • 18
  • 34