Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unbold the selected text in Edittext Android?

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.**
            }
        });
like image 200
Nik88 Avatar asked May 23 '12 06:05

Nik88


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

    }
});    
like image 167
Mohammed Azharuddin Shaikh Avatar answered Oct 21 '22 04:10

Mohammed Azharuddin Shaikh