Warm tip: This article is reproduced from stackoverflow.com, please click
android kotlin dialogfragment

How can I make specific text argument bold in android dialog fragment

发布于 2020-03-31 23:00:34

I would like to make the message arguments text bold given the following string file.how can i make it?

code is given blow:

<string name="meter_reading_dialog_message">Current Reading: %s m³ \nPrevious Reading: %s m³ \nConsumption Reading: %s m³</string>

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val toCurrentReading =
            requireArguments().getString(CURRENT_READING)
        val toPreviousReading =
            requireArguments().getString(PREVIOUS_READING)
        val toConsumption =
            requireArguments().getString(CONSUMPTION)

        return AlertDialog.Builder(requireContext())
            .setTitle(getString(R.string.confirmation_reading))
            .setMessage(
                getString(
                    R.string.meter_reading_dialog_message,
                    toCurrentReading,
                    toPreviousReading,
                    toConsumption
                )
            )
            .setPositiveButton(R.string.deactivate_save) { _, _ ->
                targetFragment?.onActivityResult(targetRequestCode, Activity.RESULT_OK, null)
            }
            .setNegativeButton(android.R.string.cancel) { _, _ ->
                targetFragment?.onActivityResult(targetRequestCode, Activity.RESULT_CANCELED, null)
            }
            .create()
    }
Questioner
Sefu Zephaniah
Viewed
48
mrtcnkryln 2020-01-31 20:03

I think you can use this for your situation.

you first define a string with it.

val mystring = getString(
                    R.string.meter_reading_dialog_message,
                    toCurrentReading,
                    toPreviousReading,
                    toConsumption
                )

After that, you can set bold value like in this example.

How to set part of text to bold when using AlertDialog.setMessage() in Android?