Vik0809
Vik0809

Reputation: 379

how to set selected Text bold?

I want to set a selected text in an EditText bold.

It's already possible to find out what characters I selected and with getSelectionStart() and getSelectionEnd( ) I know where's the position.

But my problem is, that I want to set the selected Text bold with a Button and don't know how to set the font bold with a String (I only know how to set it with EditText).

So here is my sample code:

    fettdruckButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            int selectionStart = fullscreenEdittext.getSelectionStart();
            int selectionEnd = fullscreenEdittext.getSelectionEnd();

            int differenz = selectionEnd - selectionStart;

            String selectedText = fullscreenEdittext.getText().toString().substring(selectionStart, selectionEnd);
            Log.d("selectedText", "selectedText " + selectedText + "|" + selectionStart + "|" + selectionEnd);


            //fullscreenEdittext.setTypeface(null, Typeface.BOLD);

        }
    });

Upvotes: 2

Views: 2116

Answers (3)

Mahesh
Mahesh

Reputation: 984

Use the property of Editext fullscreenEdittext.setText(Html.fromHtml(styledText)); Use html formatting tags <b>selectedText</b> to make the selected text bold. Please have a look at below code snippet

int selectionStart = fullscreenEdittext.getSelectionStart();
    int selectionEnd = fullscreenEdittext.getSelectionEnd();

    String startingText = fullscreenEdittext.getText().toString()
            .substring(0, selectionStart);
    String selectedText = fullscreenEdittext.getText().toString()
            .substring(selectionStart, selectionEnd);
    String endingText = fullscreenEdittext.getText().toString()
            .substring(selectionEnd);

    fullscreenEdittext.setText(Html.fromHtml(startingText + "<b>"
            + selectedText + "</b>" + endingText));

Upvotes: 0

Okas
Okas

Reputation: 2674

Use SpannableStringBuilder.

SpannableStringBuilder stringBuilder = (SpannableStringBuilder) fullscreeneditText.getText();
stringBuilder.setSpan(new StyleSpan(Typeface.BOLD), selectionStart, selectionEnd, 0);

Please note that if you want to do this many times you should remove your original span first.

Upvotes: 1

TechArcSri
TechArcSri

Reputation: 2002

String completetext=fullscreenEdittext.getText().toString();
int selectionStart = fullscreenEdittext.getSelectionStart();
int selectionEnd = fullscreenEdittext.getSelectionEnd();
int differenz = selectionEnd - selectionStart;
String selectedText = fullscreenEdittext.getText().toString().substring(selectionStart, selectionEnd);
Log.d("selectedText", "selectedText " + selectedText + "|" + selectionStart + "|" + selectionEnd);

String part1=fullscreenEdittext.getText().toString().substring(0, selectionStart);

String part2=fullscreenEdittext.getText().toString().substring(selectionStart, selectionEnd);

String part3=fullscreenEdittext.getText().toString().substring(selectionEnd,completetext.length() );

fullscreenEdittext.setText(Html.fromHtml(part1+"<b>" + part2+ "</b>" +part3));

Upvotes: 0

Related Questions