How to unbold the selected text in Edittext Android?
Asked Answered
N

4

7

I am working with an edit text to support the properties of bold,italic and underline.I got succeed after selecting the text and make it bold. Now I want to remove the bold after clicking on Normal button.

Typeface.NORMAL is not working at here. Can any one suggest other option.

Button btnBold = (Button) findViewById(R.id.btnBold);
        btnBold.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startSelection = etx.getSelectionStart();
                endSelection = etx.getSelectionEnd();


                Spannable s = etx.getText();
                s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startSelection, endSelection, 0);
            }
        });


Button btnNormal = (Button) findViewById(R.id.btnNormal );
        btnNormal .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                **//What I have to do here.**
            }
        });
Nutty answered 23/5, 2012 at 6:32 Comment(2)
Check this post, you will figure the rest out. #37002677Melone
I am facing the same issue that selected text is not updating, whole string updatingHyrup
F
7
Button btnNormal = (Button) findViewById(R.id.btnNormal );
        btnNormal .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               Spannable str = etx.getText();
               StyleSpan[] ss = str.getSpans(selectionStart, selectionEnd, StyleSpan.class);

       for (int i = 0; i < ss.length; i++) {
           if (ss[i].getStyle() == android.graphics.Typeface.BOLD){
            str.removeSpan(ss[i]);          
           }
       }
    etx.setText(str);

    }
});    
Freyah answered 23/5, 2012 at 6:47 Comment(1)
Any one has a clue, why just setting the type face to NORMAL is not working?Insignificance
C
0

in my project I use this construction

textView.typeface = Typeface.create(textView.typeface, Typeface.NORMAL)
Childe answered 24/12, 2018 at 17:6 Comment(0)
R
-1

Similar to what you have used in first onClick() instead of s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startSelection, endSelection, 0); use s.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), startSelection, endSelection, 0); in the second onclick().

Regality answered 23/5, 2012 at 6:39 Comment(2)
I have already mention in question that Typeface.NORMAL is not working.Nutty
Sorry for that, but i posted this answer before you edited your question.Regality
N
-2

Simply use:

Typeface.NORMAL

You can check it on the Android docs.

Nakisha answered 23/5, 2012 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.