17

Say I have an Earthquake class which has a field public final double magnitude;, and I have a layout similar to this one:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="earthquake" type="com.example.Earthquake"/>
    </data>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@{String.format(earthquake.magnitude)}" />
        ...
    </LinearLayout>
</layout>

Note I have to use "@{String.format(earthquake.magnitude)}" to use this field, else I get this error:

Cannot find the setter for attribute 'android:text' on android.widget.TextView with parameter type double.

Unfortunately, this results in the double being printed at full accuracy. How can I format the double value that is shown?

Adam S
  • 16,144
  • 6
  • 54
  • 81
  • 3
    Another approach, besides your accepted answer, is to consider binding not the `Earthquake`, but perhaps something more along the lines of an `EarthquakeViewModel`. I'm *far* from an MVVM expert, but my interpretation of the "view model" is that it has a representation of the data that will be used by the views. `EarthquakeViewModel` might have `getMagnitude()` that return the formatted string, so formatting logic is kept out of the layout XML resources. I'm not saying this is better or worse, but it was my original reaction upon seeing your question, before getting to the answer. – CommonsWare May 31 '15 at 20:22

3 Answers3

38

I haven't yet looked at the binding expression language in the M SDK preview, so I might be jumping to conclusions, but if this calls through to the normal String.format() method, then it requires a pattern. Have you tried this?

android:text='@{String.format("%.1f", earthquake.magnitude)}'
Barend
  • 17,296
  • 2
  • 61
  • 80
  • 2
    Beat me to answering my own question! I didn't realise I could use single quotes for the XML attribute values. I found it described under [Expression Language#String Literals](https://developer.android.com/tools/data-binding/guide.html#expression_language) in the docs. – Adam S May 31 '15 at 20:09
1

String.valueOf() is also an option:

android:text="@{String.valueOf(earthquake.magnitude)}"
Paul Spiesberger
  • 5,630
  • 1
  • 43
  • 53
0

String.format not working inside xml for me. I created getter method for it which value to be formatted.

data class CrpyoCoin(
    val id: Long,
    val symbol: String,
    val iconUrl: String,
    val price: Double,
    val change: Float
) {
    val readablePrice: String
        get() = String.format("%.2f", price)

}

After then i just call readable value. I think its more clear way to handling data.

  <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@{coin.readablePrice}"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/guideline"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="121.12" />
Gökberk Yağcı
  • 376
  • 1
  • 4
  • 10