'How to display text like (5% off) using data binding in android?

I need to display text in text view (5% Off) using databinding. My code is

<com.google.android.material.textview.MaterialTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_5"
            android:text="@{@string/percentage_off(priceUIModel.discountOfferPercentage)}"
            android:textAppearance="@style/Heading2Lines.Size12.Light"
            android:textColor="@color/red_100"
            app:visibility="@{priceUIModel.discountOfferPercentage !=null}" />

In string.xml

<string name="percentage_off">%s\%% Off</string>

Simply need to add brackets front and back of the text. How can I modify this string.xml? Thanks in advance.



Solution 1:[1]

Use the String.format() command:

android:text='@{String.format("(%s)",@string/percentage_off(priceUIModel.discountOfferPercentage))}'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cheticamp