0

There's a similar answer here, but it doesn't state what I'm about to ask.

I have a 'Bold Text' option in my Android view and if a user selects that option, a boolean gets set as true in the code behind.

Using a TextWatcher, how can I change the text typed by the user to bold after a specific point in the EditText. If the user turns it off, the text typed after should be in normal styling. Everything depends on the boolean value.

Here's what I have so far:

Boolean isBolded = false;

// Code that turns the bold option true and false...

contentBox = (EditText) findViewById(R.id.contentBox);
contentBox.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(isBolded)
                {
                   //Start bolding the text typed after that point
                }
                else
                {
                   //Stop styling the text typed after that point
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

The difficulty would be to determine the points in the EditText where the user decides to turn bolding off and on while typing. What do you guys have in mind?

Community
  • 1
  • 1
Jay
  • 4,873
  • 7
  • 72
  • 137
  • Here is a basic implementation of a text watcher I hope it will help you http://stackoverflow.com/questions/33645769/using-a-textwatcher-on-a-edittext-to-calculate-the-total-and-sum-total-in-real-t Your logic for making the text bold goes inside afterTextChanged() – AndroidNewBee Nov 29 '15 at 16:59

1 Answers1

0

Set Html.fromHtml("<b>This part will be bold</b> This won't!") as the Text.

Empty2k12
  • 475
  • 12
  • 34
  • My problem is finding out where the user decides to switch on bold and where he turns it off and getting that position in the edittext – Jay Nov 30 '15 at 00:39