Nicola Gallazzi
Nicola Gallazzi

Reputation: 8713

textStyle="bold" not working in DialogFragment

I'm struggling with a custom DialogFragment layout defined as below:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="360dp"
    android:background="@color/colorDarkerGreen"
    android:minWidth="280dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/ivNoRetails"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="24dp"
        android:scaleType="centerInside"
        android:src="@drawable/no_retails_nearby_dialog"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="24dp"
        android:text="@string/no_retails_nearby"
        android:textAlignment="center"
        android:textColor="@color/colorBaseWhite"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ivNoRetails" />

    <TextView
        android:id="@+id/tvHopeGetThere"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="@string/hope_we_get_there"
        android:textAlignment="center"
        android:textColor="@color/colorBaseWhite"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@+id/textView2"
        app:layout_constraintStart_toStartOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btClose"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:text="@string/understood"
        android:textColor="@color/colorBaseWhite"
        app:backgroundTint="@android:color/transparent"
        app:cornerRadius="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvHopeGetThere"
        app:strokeColor="@color/colorBaseWhite"
        app:strokeWidth="2dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

I the preview from the layout editor, I can see the text of tvHopeGetThere TextView displayed as bold, as expected. However, when I show the dialog in my activity, the text is displayed as with a normal textStyle.

enter image description here

DialogFragment:

class NoRetailsNearbyDialogFragment : DialogFragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.no_retails_alert_dialog, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        btClose.setOnClickListener {
            dismiss()
        }
    }

    companion object {
        fun newInstance(): NoRetailsNearbyDialogFragment {
            return NoRetailsNearbyDialogFragment()
        }
    }

}

Display of the dialog in my fragment:

  val fragment = NoRetailsNearbyDialogFragment.newInstance()
                fragment.show(fragmentManager, "")

I really can't understand why the textview has this strange behavior. enter image description here

Upvotes: 1

Views: 575

Answers (2)

Nicola Gallazzi
Nicola Gallazzi

Reputation: 8713

Problem was related to Calligraphy library that was overriding common textStyle behavior. Once I removed it, everything started to work fine :)

Upvotes: 0

Tanveer singh Bhatia
Tanveer singh Bhatia

Reputation: 69

there is one method, you can set textview typeface by calling setTypeface method, see the following your can do it programatically as:

textView.setTypeface(null, Typeface.BOLD);

or you can do it by html approach as:

TextView t = new TextView(mContext);
t.setText(Html.fromHtml("<b>This is bold</b>"));

Upvotes: 1

Related Questions