Skip to content

Commit 7952a3b

Browse files
committed
add highlight while text changing option | update readme file
1 parent bba0a5f commit 7952a3b

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,10 @@ Set highlighter update delay
142142
codeView.setUpdateDelayTime();
143143
```
144144

145+
Enable/Disable highlight the code while the text is changing
146+
147+
```java
148+
codeView.setHighlightWhileTextChanging(enableHighlighter);
149+
```
150+
145151
#### For real examples on how to use CodeView check the example app

codeview/src/main/java/com/amrdeveloper/codeview/CodeView.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ public class CodeView extends AppCompatMultiAutoCompleteTextView {
3939
private int mUpdateDelayTime = 500;
4040

4141
private boolean modified = true;
42-
private boolean hasErrors;
43-
private boolean mRemoveErrorsWhenTextChanged;
42+
private boolean highlightWhileTextChanging = true;
43+
44+
private boolean hasErrors = false;
45+
private boolean mRemoveErrorsWhenTextChanged = true;
4446

4547
private final Handler mUpdateHandler = new Handler();
4648
private MultiAutoCompleteTextView.Tokenizer mAutoCompleteTokenizer;
@@ -194,8 +196,9 @@ private void createBackgroundColorSpan(Editable editable, Matcher matcher, @Colo
194196
}
195197

196198
private Editable highlight(Editable editable) {
199+
if(editable.length() == 0) return editable;
200+
197201
try {
198-
if(editable.length() == 0) return editable;
199202
clearSpans(editable);
200203
highlightErrorLines(editable);
201204
highlightSyntax(editable);
@@ -213,7 +216,7 @@ private void highlightWithoutChange(Editable editable) {
213216
}
214217

215218
public void setTextHighlighted(CharSequence text) {
216-
if (text == null) text = "";
219+
if (text == null || text.length() == 0) return;
217220

218221
cancelHighlighterRender();
219222

@@ -349,6 +352,10 @@ public int getUpdateDelayTime() {
349352
return mUpdateDelayTime;
350353
}
351354

355+
public void setHighlightWhileTextChanging(boolean updateWhileTextChanging) {
356+
this.highlightWhileTextChanging = updateWhileTextChanging;
357+
}
358+
352359
@Override
353360
public void showDropDown() {
354361
int[] screenPoint = new int[2];
@@ -390,20 +397,29 @@ public void beforeTextChanged(CharSequence charSequence, int start, int before,
390397

391398
@Override
392399
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
400+
if (!modified) return;
401+
402+
if(highlightWhileTextChanging) {
403+
if (mSyntaxPatternMap.size() > 0) {
404+
convertTabs(getEditableText(), start, count);
405+
mUpdateHandler.postDelayed(mUpdateRunnable, mUpdateDelayTime);
406+
}
407+
}
408+
409+
if (mRemoveErrorsWhenTextChanged) removeAllErrorLines();
393410
}
394411

395412
@Override
396413
public void afterTextChanged(Editable editable) {
397-
cancelHighlighterRender();
398-
399-
if(getSyntaxPatternsSize() > 0) {
400-
convertTabs(editable, start, count);
401-
414+
if(!highlightWhileTextChanging) {
402415
if (!modified) return;
403416

404-
mUpdateHandler.postDelayed(mUpdateRunnable, mUpdateDelayTime);
417+
cancelHighlighterRender();
405418

406-
if (mRemoveErrorsWhenTextChanged) removeAllErrorLines();
419+
if (mSyntaxPatternMap.size() > 0) {
420+
convertTabs(getEditableText(), start, count);
421+
mUpdateHandler.postDelayed(mUpdateRunnable, mUpdateDelayTime);
422+
}
407423
}
408424
}
409425
};

0 commit comments

Comments
 (0)