Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make bold text with Android Data Binding Library

Pretty basic, I want to make a title of a message bold based on whether the text it is read or not. I can't seem to find a solution for this.

Here is my XML code:

            <TextView
                android:text="@{message.title}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_toLeftOf="@+id/timestamp"
                android:textSize="18sp"
                android:textStyle='@{message.isRead() ? "bold" : "normal"}'
                android:textColor='@{message.isRead() ? 0xff313131 : 0xff0662ab}' />

Th colorchange is working great, only the bold text is giving me some problems.

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Cannot find the setter for attribute 'android:textStyle' with parameter type java.lang.String on android.widget.TextView. file:D:......xml loc:39:41 - 39:79 ****\ data binding error ****

like image 532
Saif Bechan Avatar asked Jun 03 '16 08:06

Saif Bechan


People also ask

How do I make text bold on android?

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 bold text in android XML?

Change Text Style of TextView to BOLD in XML Layout File textStyle attribute of TextView widget accepts on of these values: "bold" , "italic" or "normal" . To change the style to bold, you have to assign textStyle with "bold" .

How do you bold and italicize text on android?

Android: Tap and hold the text you're entering in the text field, then choose Bold, Italic, or More . Tap More to choose Strikethrough or Monospace.

Is data binding good in android?

Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.


2 Answers

An easy way

public class TextViewBindingAdapter {
    @BindingAdapter("isBold")
    public static void setBold(TextView view, boolean isBold) {
        if (isBold) {
            view.setTypeface(null, Typeface.BOLD);
        } else {
            view.setTypeface(null, Typeface.NORMAL);
        }
    }
}

XML:

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:isBold="@{item.bold}"/>
like image 121
tomrozb Avatar answered Sep 21 '22 01:09

tomrozb


I ended up using the following code, it implements DataBinding.

public abstract class BindingAdapter {
    @android.databinding.BindingAdapter("android:typeface")
    public static void setTypeface(TextView v, String style) {
        switch (style) {
            case "bold":
                v.setTypeface(null, Typeface.BOLD);
                break;
            default:
                v.setTypeface(null, Typeface.NORMAL);
                break;
        }
    }
}

And the XML

<TextView
    android:text="@{bericht.titel}"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:layout_toLeftOf="@+id/timestamp"
    android:textSize="18sp"
    android:textColor='@{bericht.isGelezen() ? 0xff313131 : 0xff0662ab}'
    android:typeface='@{bericht.isGelezen() ? "normal" : "bold"}' />
like image 25
Saif Bechan Avatar answered Sep 23 '22 01:09

Saif Bechan