Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make certain part of text bold using DataBinding

I want to make certain part of my text to bold whose value is set using DataBinding with ViewModel.

For e.g

If you are selected, you will pay $160 for your pair.

I am using strings resources

<string name="product_price">If you are selected, you will have to pay $%d for your pair.</string>

<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="@{@string/product_price(productPrice)}"
          android:textColor="@color/button_tertiary"
          android:visibility="@{productPrice > 0}"
          style="@style/Body.Small"
          />

Currently passing product price using ViewModel with Binding by setting binding.setProductPrice(Object.getPrice())

I know the following solutions : But want to try using DataBinding

  • Using Html Text - But don't want to use it in code.
  • Using Different TextView in Horizontal Style. Setting styles as bold for that Product Price. - Really Bad Practice
  • Using SpannableString - But don't want to use it in code.

But all of the above solutions are workaround.

Question ::

Want to try DataBinding feature which can be used to style certain part of string. Just like SpannableString

Manipulate String in the Layout file using DataBinding

like image 263
Vikalp Patel Avatar asked Aug 13 '18 12:08

Vikalp Patel


People also ask

How do I make part of a string bold?

android:textStyle attribute is the first and one of the best way to make the text in TextView bold. just use “bold”. If you want to use bold and italic. Use pipeline symbol “|” .

How do I make part of a string bold in Java?

16 + names. length() is where to stop bolding. So I said start bolding after "You have chosen " and stop bolding the length of the name positions after where it started. b is the type of span to apply on the StringBuilder (which is bold).

How do I make bold text in Spannable?

text. style. StyleSpan(Typeface. BOLD), start, end, Spannable.

How do I make a string bold in XML?

Just use getText(R. string.


1 Answers

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.

like image 58
Mahavir Jain Avatar answered Oct 15 '22 08:10

Mahavir Jain