-1

I want to style few parts of texts in textView with bold (or) italics. (through strings.xml)

AndroidDev
  • 1,485
  • 2
  • 18
  • 33
  • 1
    Does this answer your question? [Set TextView style (bold or italic)](https://stackoverflow.com/questions/6200533/set-textview-style-bold-or-italic) – NPovlsen Oct 30 '19 at 06:54
  • You can use **Spannable String** to change text style dynamically. – Jahanvi Kariya Oct 30 '19 at 06:57
  • If you are looking to do it at run time you can do it with SpannableString , have a look at this https://stackoverflow.com/a/58345640/11166067 – Dmitrii Leonov Oct 30 '19 at 06:59
  • @NPovlsen, no that question asks specifically how to do it via java not through xml. But my case is different, just letting know there is alternate simple way as well. – AndroidDev Oct 30 '19 at 07:04
  • @Velu not even [this answer](https://stackoverflow.com/a/6200598/9261932)? I know that it is java too, but it is a simple way – NPovlsen Oct 30 '19 at 07:09
  • @NPovlsen, Yes it's in java and simple way compared to other suggested answer. But my requirement is to do it at compile time itself. And it also helps in localisation if the strings are in xml. Thanks. – AndroidDev Oct 30 '19 at 07:21

2 Answers2

2

Create a class StringUtil

class StringUtil {

    lateinit var ss: SpannableString
    fun addForegroundColorSpan(color: Int, start: Int, end: Int): StringUtil {
        ss.setSpan(ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return ssu
    }
    fun addStyleSpan(style: Int, start: Int, end: Int): StringUtil {
        ss.setSpan(StyleSpan(style), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return ssu
    }

    fun addClickableSpan(cs: ClickableSpan, start: Int, end: Int): StringUtil {
        ss.setSpan(cs, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return ssu
    }

    fun setMovementMethod(textview: TextView): StringUtil {
        textview.setMovementMethod(LinkMovementMethod.getInstance())
        return ssu
    }

    fun getClickableSpan(url: String,context: Context): ClickableSpan {
        return object : ClickableSpan() {
            override fun onClick(widget: View) {
                val uri: Uri = Uri.parse(url)
                val intent = Intent(Intent.ACTION_VIEW, uri)
                context.startActivity(intent);
            }
        }
    }

    fun setSpannableString(textview: TextView) {
        textview.text = ss
    }

    fun getSS(text: String): StringUtil {
        ss = SpannableString(text)
        return ssu
    }

    fun setPrefixString(edt: EditText, prefixString: String) {
        edt.setText(prefixString)
        Selection.setSelection(edt.getText(), edt.getText().length);

        edt.addTextChangedListener(object : TextWatcher {

            override fun afterTextChanged(s: Editable) {
                if(!s.toString().startsWith(prefixString)){
                    edt.setText(prefixString);
                    Selection.setSelection(edt.getText(), edt.getText().length);

                }

            }

            override fun beforeTextChanged(
                s: CharSequence, start: Int,
                count: Int, after: Int
            ) {
            }

            override fun onTextChanged(
                s: CharSequence, start: Int,
                before: Int, count: Int
            ) {

            }
        })
    }

    companion object {
        private var ssu: StringUtil = StringUtil()
        fun getInstance(): StringUtil {
            return ssu
        }

    }
}

Use it like

StringUtil.getInstance().getSS("Here your text. You can set color Span , Style Span, Click Span for the specific part of your string.")
        .addStyleSpan(Typeface.BOLD,34,39)
        .addClickableSpan(StringUtil.getInstance().getClickableSpan("http://www.google.com",this), 34, 39)
        .addForegroundColorSpan(Color.YELLOW, 34, 39)
        .addStyleSpan(Typeface.BOLD,43,text.length-1)
        .addClickableSpan(StringUtil.getInstance().getClickableSpan("http://www.facebook.com",this), 43, text.length-1)
        .addForegroundColorSpan(Color.YELLOW, 43, text.length-1 )
        .setMovementMethod(binding.tvWarringPrivacyPolicy)
        .setSpannableString(binding.tvWarringPrivacyPolicy)
Ashish
  • 6,791
  • 3
  • 26
  • 48
1

Android textView supports lightweight HTML markup: <b>, <u>, <i>

<resources>
    <string name="b">This has <b>bold</b> in it.</string>
    <string name="i">Whereas this has <i>italics</i>!</string>
</resources>
AndroidDev
  • 1,485
  • 2
  • 18
  • 33