change the specific word color when typing in Edit text or MultiAutoCompleteTextView with file extention

181 Views Asked by At

I create small code editor app.Problem is, changing the extention is not change specific words color.I use string array resources file to identify specific word. and also add Common class to get extention using save as dialog. In Common class i got a problem using below,

public static String currentExtention;

Null point Exception error.then i set a value for it like "txt". But now problem is doesn't change color when i change the extention. My classes are below.

public class Common {
    public static String currentExtention="txt";
    public static void setCurrentExtention(String extention)
    {
       currentExtention=extention;
    }

    public static String getCurrentExtention()
    {
       return currentExtention;
    }
} 

I change the data type using FileSaveDialog class.using

Common.setCurrentExtention(extention );

extention is, set values get by user. it is work and pass Main activity to correct extention. In my Activity class is below

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_file);

    txtnumberView = (TextView) findViewById(R.id.numberViewText);
    edtTextView = (MultiAutoCompleteTextView) findViewById(R.id.edtTextView);
    relativeLayout = (RelativeLayout) findViewById(R.id.layout_root);
    helper = new TextViewUndoRedo(edtTextView);
    context = NewFileActivity.this;
    fileSaveDialog = new FileSaveDialog(context);
    findTextDialog = new FindTextDialog(context);
    autoCompleteText = new AutoCompleteText(context);
    autoChangeNumberTxtView=new AutoChangeNumberTxtView(context,edtTextView,txtnumberView);

     switch (Common.getCurrentExtention()) {
        case "html":
            dataType = getResources().getStringArray(R.array.html);
            break;
        case "txt":
            dataType = getResources().getStringArray(R.array.txt);
            break;
    }
    regex = new StringBuilder("\\b(");
    for (String word : dataType) {
        regex.append(Pattern.quote(word));
        regex.append("|");
    }
    regex.setLength(regex.length() - 1); // delete last added "|"
    regex.append(")\\b");
    edtTextView.addTextChangedListener(new TextWatcher() {
        ColorScheme keywords = new ColorScheme(

                Pattern.compile(regex.toString()),
                Color.CYAN
        );

        ColorScheme numbers = new ColorScheme(
                Pattern.compile("(\\b(\\d*[.]?\\d+)\\b)"),
                Color.BLUE
        );

        final ColorScheme[] schemes = {keywords, numbers};

        void removeSpans(Editable e, Class<? extends CharacterStyle> type) {
            CharacterStyle[] spans = e.getSpans(0, e.length(), type);
            for (CharacterStyle span : spans) {
                e.removeSpan(span);
            }
        }

        class ColorScheme {
            final Pattern pattern;
            final int color;

            ColorScheme(Pattern pattern, int color) {
                this.pattern = pattern;
                this.color = color;
            }
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            autoChangeNumberTxtView.autoOnchange();
            textInType();


        }

        @Override
        public void afterTextChanged(Editable editable) {

                removeSpans(editable, ForegroundColorSpan.class);
                for (ColorScheme scheme : schemes) {
                    for (Matcher m = scheme.pattern.matcher(editable); m.find(); ) {
                        editable.setSpan(new ForegroundColorSpan(scheme.color),
                                m.start(),
                                m.end(),
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }


            }
        }

    });

For my further development I use MultiAutoCompleteTextView instead of EditText. I think problem in there.

  switch (Common.getCurrentExtention()) {
        case "html":
            dataType = getResources().getStringArray(R.array.html);
            break;
        case "txt":
            dataType = getResources().getStringArray(R.array.txt);
            break;
    }
    regex = new StringBuilder("\\b(");
    for (String word : dataType) {
        regex.append(Pattern.quote(word));
        regex.append("|");
    }
    regex.setLength(regex.length() - 1); // delete last added "|"
    regex.append(")\\b");
    edtTextView.addTextChangedListener(new TextWatcher() {
        ColorScheme keywords = new ColorScheme(

                Pattern.compile(regex.toString()),
                Color.CYAN
        );

        ColorScheme numbers = new ColorScheme(
                Pattern.compile("(\\b(\\d*[.]?\\d+)\\b)"),
                Color.BLUE
        );

        final ColorScheme[] schemes = {keywords, numbers};

        void removeSpans(Editable e, Class<? extends CharacterStyle> type) {
            CharacterStyle[] spans = e.getSpans(0, e.length(), type);
            for (CharacterStyle span : spans) {
                e.removeSpan(span);
            }
        }

        class ColorScheme {
            final Pattern pattern;
            final int color;

            ColorScheme(Pattern pattern, int color) {
                this.pattern = pattern;
                this.color = color;
            }
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {

                removeSpans(editable, ForegroundColorSpan.class);
                for (ColorScheme scheme : schemes) {
                    for (Matcher m = scheme.pattern.matcher(editable); m.find(); ) {
                        editable.setSpan(new ForegroundColorSpan(scheme.color),
                                m.start(),
                                m.end(),
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }

This is not crash but logcat display below . when i change the file extention.

E/ViewRootImpl: sendUserActionEvent() mView == null
E/ViewRootImpl: sendUserActionEvent() mView == null

If someone can explain me what is the problem or what is the correct way to do this.it will be very helpful to me.

0

There are 0 best solutions below