My CodeEditor Android app becomes slow because the algorithm check for words to highlight every time a letter is written. How can i fix it? -
after few minutes of writing becomes slow because algorithm keeps controlling whole written text looking words highlight. problem checks text every time letter written.
how can change algorithm make app faster , usable?
@override public void aftertextchanged(editable s) { string input = s.tostring(); string[] words = input.split(space); int start = 0; for(int i=0;i<words.length ;i++){ string word = words[i]; if(map.containskey(word)){ int index = input.indexof(word, start); string color = map.get(word); s.setspan(new foregroundcolorspan(color.parsecolor(color)), index, index+word.length(), spanned.span_exclusive_exclusive); start = index+word.length(); } }
here whole activitymain of app.
package com.example.android.editor; import android.app.activity; import android.content.intent; import android.graphics.color; import android.os.bundle; import android.text.editable; import android.text.spanned; import android.text.textutils; import android.text.textwatcher; import android.text.style.foregroundcolorspan; import android.view.view; import android.widget.edittext; import java.util.hashmap; import java.util.map; import static com.example.android.editor.r.id.edittext; /** * ------------ * 30/03/2017 * ------------ * */ public class mainactivity extends activity { string projecttitle; //send code via social networks public void invia(view view) { string sharebody = "file: " + edittexttitle.gettext().tostring() + "\n\n" +edittextbody.gettext().tostring(); intent sharingintent = new intent(android.content.intent.action_send); sharingintent.settype("text/plain"); sharingintent.putextra(android.content.intent.extra_text, sharebody); startactivity(intent.createchooser(sharingintent, getresources().getstring(r.string.app_name))); } public void save(view view) { string title = edittexttitle.gettext().tostring(); string content = edittextbody.gettext().tostring(); pref.setprefs(pref.sharedprefsprojects); startactivity(new intent(this, selectproject.class)); if(!textutils.isempty(title) && !textutils.isempty(content)) { if(!textutils.isempty(projecttitle)) { pref.removevalue(content); pref.savestringvalue(title, content); if(!projecttitle.equals(title)) { pref.removevalue(projecttitle); pref.removevalue(content);//adri pref.savestringvalue(title, content); } } else { pref.removevalue(content);//adri pref.savestringvalue(title, content); } startactivity(new intent(this, selectproject.class)); } } //this function make possible insert tab public void tab(view view) { edittextbody.gettext().insert(edittextbody.getselectionstart(), "\t\t\t\t"); } map<string, string> map = new hashmap<string, string>(); edittext edittexttitle, edittextbody; sharedpref pref; private final static string space = "\\s"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edittexttitle = (edittext)findviewbyid(r.id.titolo); edittextbody = (edittext)findviewbyid(edittext); pref = new sharedpref(this); //dictionary of words highlighted , colors map.put("if", "#00e676"); map.put("else", "#00e676"); map.put("for", "#00e676"); map.put("while", "#00e676"); map.put("do", "#00e676"); map.put("int", "#e91e63"); map.put("void", "#e91e63"); map.put("sizeof", "#e91e63"); map.put("char", "#e91e63"); map.put("bool", "#e91e63"); map.put("float", "#e91e63"); map.put("double", "#e91e63"); map.put("unsigned", "#e91e63"); map.put("+", "#e91e63"); map.put("-", "#e91e63"); map.put("=", "#e91e63"); map.put("false", "#2196f3"); map.put("true", "#2196f3"); map.put("main", "#2196f3"); map.put("<stdio.h", "#2196f3"); map.put("<stdlib.h", "#2196f3"); map.put("printf", "#2196f3"); map.put("scanf", "#2196f3"); map.put("alloc", "#2196f3"); map.put("malloc", "#2196f3"); map.put("realloc", "#2196f3"); map.put("%c", "#673ab7"); map.put("%d", "#673ab7"); map.put("%f", "#673ab7"); map.put("(", "#ffeb3b"); map.put(")", "#ffeb3b"); map.put("(){", "#ffeb3b"); map.put("()", "#ffeb3b"); map.put("{", "#ffeb3b"); map.put("}", "#ffeb3b"); map.put("1", "#7c4dff"); map.put("2", "#7c4dff"); map.put("3", "#7c4dff"); map.put("4", "#7c4dff"); map.put("5", "#7c4dff"); map.put("6", "#7c4dff"); map.put("7", "#7c4dff"); map.put("8", "#7c4dff"); map.put("9", "#7c4dff"); map.put("0", "#7c4dff"); map.put("*", "#ffffff"); edittextbody.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) { } @override public void aftertextchanged(editable s) { string input = s.tostring(); string[] words = input.split(space); //function highlight words //here problem int start = 0; for(int i=0;i<words.length ;i++){ string word = words[i]; if(map.containskey(word)){ int index = input.indexof(word, start); string color = map.get(word); s.setspan(new foregroundcolorspan(color.parsecolor(color)), index, index+word.length(), spanned.span_exclusive_exclusive); start = index+word.length(); } } } }); if(getintent() != null) { projecttitle = getintent().getstringextra("title"); string projectbody = getintent().getstringextra("body"); edittextbody.settext(projectbody); edittexttitle.settext(projecttitle); } } }
if write down code great help.
instead of using array store , re-scan word check last word
public void aftertextchanged(editable s) { string input = s.tostring(); string word = input.substring(input.lastindexof("\s")); int index = input.lastindexof("\s"); if(map.containskey(word)){ string color = map.get(word); s.setspan(new foregroundcolorspan(color.parsecolor(color)), index, index+word.length(), spanned.span_exclusive_exclusive); }
this should work done.
Comments
Post a Comment