Make certain part of text bold using DataBinding
You have to create a BindingAdapter
and SpannableStringBuilder
.
Binding Adapter
object Util {
@BindingAdapter("main","secondText")
@JvmStatic
fun setBoldString(view: AppCompatTextView, maintext: String,sequence: String) {
view.text = Util.getBoldText(maintext, sequence)
}
@JvmStatic
fun getBoldText(text: String, name: String): SpannableStringBuilder {
val str = SpannableStringBuilder(text)
val textPosition = text.indexOf(name)
str.setSpan(android.text.style.StyleSpan(Typeface.BOLD),
textPosition, textPosition + name.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
return str
}
}
XML
<android.support.v7.widget.AppCompatTextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:main="@{`you will pay $160 for your pair`}"
app:secondText="@{`$160`}"
android:textColor="@color/black"
android:textSize="22sp" />
May be it helps you.
As per @CommonsWare,
Tried by adding basic Html tag <string name="product_price">If you are selected, you will have to pay <![CDATA[<b>$%d</b>]]> for your pair.</string>
Layout File : Imported Html
<?xml version="1.0" encoding="utf-8"?>
<layout
<data>
<import type="android.text.Html"/>
<data>
<LinearLayout>
<android.support.design.widget.CoordinatorLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/spacing_xlarge"
android:layout_marginStart="@dimen/spacing_xlarge"
android:layout_marginBottom="@dimen/spacing_small"
android:text="@{Html.fromHtml(@string/product_price(productPrice))}"
android:textColor="@color/button_tertiary"
android:visibility="@{productPrice > 0}"
style="@style/Body.Small"
/>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
</layout>