How to unlock selected text in Edittext Android?

I work with edit text to support bold, italic, and underline properties. I managed to succeed after selecting the text and making it bold. Now I want to remove the bold font after clicking on the "Normal" button.

Typeface.NORMAL does not work here. Can anyone suggest another 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.**
            }
        });
+5
source share
3 answers
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);

    }
});    
+7
source

Just use:

Typeface.NORMAL

You can check it out on Android docs .

-1
source

Similar to what you used in the 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 ().

-1
source

All Articles