-1

i am trying to create a basic text editor. I almost achived what i wanted to do but there are some problems. When i press 'B' button, it makes text bold. If i keep writing without giving space and press 'B' button again, all letters i write turn normal. But i want to keep letters bold which are already bold. When i give space, it works as i want, makes bold, italic or normal. Here are some parts of my code:

 editor.textChangedListener {
        beforeTextChanged { _, _, _, _ ->
            val length = editor.text?.length
            length?.let {
                if (it > 0) {
                    val styleSpan = when {
                        isBold && isItalic -> StyleSpan(Typeface.BOLD_ITALIC)
                        isBold && !isItalic -> StyleSpan(Typeface.BOLD)
                        !isBold && isItalic -> StyleSpan(Typeface.ITALIC)
                        else -> StyleSpan(Typeface.NORMAL)
                    }
                    val spannable = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                    if (it < start) start = 0
                    editor.text?.setSpan(styleSpan, start, it, spannable)
                }
            }
        }
    }
    boldButton.onClick {
        isBold = !isBold
        val image = when (isBold) {
            true -> R.drawable.ic_bold_active
            false -> R.drawable.ic_bold
        }
        start = editor.text!!.length
        boldButton.setImageResource(image)
    }
Berkay Kireçci
  • 651
  • 5
  • 18
  • where is the problem?? i don't see any clear question here. – Tushar Saha Dec 31 '19 at 07:27
  • Does this answer your question? [How to set TextView textStyle such as bold, italic](https://stackoverflow.com/questions/6200533/how-to-set-textview-textstyle-such-as-bold-italic) – Mahendra Gohil Dec 31 '19 at 07:28
  • I just tried this solution. My problem is that i dont wanna change typeface for all text. When i click bold, it makes all string bold. Thats why i used setSpan with indexes. But it doesnt work either. – Berkay Kireçci Dec 31 '19 at 07:59

2 Answers2

0

To change type face you can use some thing like this.

TextView tv = (TextView) findViewById(R.id.tvname);
Typeface face = Typeface.createFromAsset(getAssets(),
        "fonts/epimodem.ttf");
tv.setTypeface(face);

Also you may try html for the same

textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
Tushar Saha
  • 1,978
  • 24
  • 29
0

You can update when change typeface when click button for bold or italic.

Add below method to your code

 fun onUpdateTypeface() {

    when {
        isBold && isItalic ->
            view?.tvForgetPassword?.setTypeface(null, Typeface.BOLD_ITALIC);
        isBold && !isItalic ->
            view?.tvForgetPassword?.setTypeface(null, Typeface.BOLD);
        !isBold && isItalic ->
            view?.tvForgetPassword?.setTypeface(null, Typeface.ITALIC);
        else -> view?.tvForgetPassword?.setTypeface(null, Typeface.NORMAL);
    }
}

Update your onClick method

boldButton.onClick {
    isBold = !isBold
    val image = when (isBold) {
        true -> R.drawable.ic_bold_active
        false -> R.drawable.ic_bold
    }
    start = editor.text!!.length
    boldButton.setImageResource(image)
    onUpdateTypeface()
}
Rajasekaran M
  • 2,478
  • 2
  • 20
  • 30