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.**
            }
        });
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Nik88
  • 1,037
  • 10
  • 21
  • Check this post, you will figure the rest out. http://stackoverflow.com/questions/37002177/how-to-bold-or-italic-or-underline-the-selected-text-in-edittext-programatically/42810064#42810064 – Bosco Mar 15 '17 at 13:06
  • I am facing the same issue that selected text is not updating, whole string updating – Nimisha V Jul 30 '21 at 12:16

4 Answers4

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);

    }
});    
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
0

in my project I use this construction

textView.typeface = Typeface.create(textView.typeface, Typeface.NORMAL)
-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().

John Joe
  • 12,412
  • 16
  • 70
  • 135
Saurabh
  • 33
  • 1
  • 5
-2

Simply use:

Typeface.NORMAL

You can check it on the Android docs.

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108