0

Am creating an application in which edittext adds style span to certain positions of text

editable.setSpan(new StyleSpan(Typeface.BOLD), start, start+replacement.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

My issue is that if user adds, some text or white space before the spanned text style will removed, how to avoid that ?

    int SPAN_COMPOSING = 256;
    int SPAN_EXCLUSIVE_EXCLUSIVE = 33;
    int SPAN_EXCLUSIVE_INCLUSIVE = 34;
    int SPAN_INCLUSIVE_EXCLUSIVE = 17;
    int SPAN_INCLUSIVE_INCLUSIVE = 18;
    int SPAN_INTERMEDIATE = 512;
    int SPAN_MARK_MARK = 17;
    int SPAN_MARK_POINT = 18;
    int SPAN_PARAGRAPH = 51;
    int SPAN_POINT_MARK = 33;
    int SPAN_POINT_MARK_MASK = 51;
    int SPAN_POINT_POINT = 34;
    int SPAN_PRIORITY = 16711680;
    int SPAN_PRIORITY_SHIFT = 16;
    int SPAN_USER = -16777216;
    int SPAN_USER_SHIFT = 24;

Which type will i need to use in my case ?

Bincy Baby
  • 3,941
  • 5
  • 36
  • 62
  • Use TextWatcher. You will get a callback for everytime your text has changed. use setSpan there. – Imdad Aug 21 '17 at 07:18
  • can you read my question again ? – Bincy Baby Aug 21 '17 at 07:32
  • 1
    I Know this maybe a bit late but you should first make sure you are spanning till editable.Length .EXCLUSIVE_EXCLUSIVE Is the best if you don't want the span to extend either left or right.However this problem can occur because of the auto-correction if you are spanning part of a word which is just stupid .You can disable the autocorrection via . `android:inputType="textMultiLine|textCapSentences|textShortMessage|textVisiblePassword"` in the EditText XML – Omar Boshra Aug 01 '18 at 18:12

1 Answers1

-1

You can use like below code.

editable.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) {
                // here is your code to change position
            }

            @Override
            public void afterTextChanged(Editable s) {

            }

        });
SWAPDROiD
  • 357
  • 3
  • 15