How to bold or italic or underline the selected text in Edittext programatically
Asked Answered
S

3

0

I have to develop an App like sticky note. User enter the text in Edittext, while selecting the text I will open the popup to select the option like (BOLD,ITALIC,UNDERLINE). when user select the options i need to change the selected text..

Does anyone go through this?

Snowdrift answered 3/5, 2016 at 10:53 Comment(6)
what you have tried so far?Catron
#10715136Canaille
i got the selected text from user. How to bold and show it in edittext?Snowdrift
may be you will get help from thishttps://github.com/wasabeef/richeditor-android OR search RichEdittext in googleUndermost
i ll check and get back to uSnowdrift
Thanks all... this link akashkubavat.wordpress.com/tag/edittext-bold and RichEdittext helped me...Snowdrift
P
2

If you have found a way the popup the options and how to retrieve the selected option(Bold, Italics, Underline), Then use this to format the text

private void formatText(EditText editText) {
    int end = editText.length();

    //get the selected text position as integer
    start = editText.getSelectionStart();
    stop = editText.getSelectionEnd();
    //check if the user started the selection from the left or the right
    if (start > stop) {
        stop = editText.getSelectionStart();
        start = editText.getSelectionEnd();
    }

        //gets the texts as html incase if previous formatting exists
    String textBefore = Html.toHtml(new SpannableString(text.getText().subSequence(0, start)));
    String selectedText = Html.toHtml(new SpannableString(text.getText().subSequence(start, stop)));
    String textAfter = Html.toHtml(new SpannableString(text.getText().subSequence(stop, end)));
    //Check if the selected text is empty ie if the did not select anything
    if (!selectedText.equals("") || start != stop) {
       //format the text
       String formatted = "<b>" + selectedText + "</b>";
       //build back the text
        StringBuilder builder = new StringBuilder();
        builder.append(textBefore);
        builder.append(Html.toHtml(formatted));
        builder.append(textAfter);

        editText.setText(Html.fromHtml(builder.toString()));
        //make the cursor stay after the selected text
        //you can also make the selected text selected here
        editText.setSelection(stop); 
        //clear your local variables
        textbefore = "";
        textafter = "";
        selectedText = "";
    }
    else {
        //you can also use snack bar here
        Toast.makeText(context, "select a text", Toast.LENGTH_SHORT).show();
    }
}

I think this should work for you

Piapiacenza answered 15/3, 2017 at 12:40 Comment(0)
N
0

Did you try this?

editText.setTypeface(null, Typeface.BOLD_ITALIC);
editText.setTypeface(null, Typeface.BOLD);
editText.setTypeface(null, Typeface.ITALIC);
editText.setTypeface(null, Typeface.NORMAL);
Namnama answered 3/5, 2016 at 10:57 Comment(0)
E
0

Kotlin

Change Typeface.BOLD, Typeface.ITALIC or UnderlineSpan()

val spannableString: Spannable = SpannableStringBuilder(editText.text)
        spannableString.setSpan(
        StyleSpan(Typeface.BOLD),
            editText.selectionStart,
            editText.selectionEnd,
            0
        )
editText.setText(spannableString)
Ethelyn answered 15/5, 2021 at 6:11 Comment(1)
It keeps formatting the whole string from the edit textRianon

© 2022 - 2024 — McMap. All rights reserved.