Thursday, 18 October 2018

Given mixed accented and normal characters in string not working in java when searching

String text = "Cámélan discovered ônte red aleŕt \n Como se extingue la deuda";

If i give the input Ca, it should highlight from the given string Cá but it's not highlighting.

Below is what I tried.

 Pattern mPattern; 
  String filterTerm; //this is the input which I give from input filter. Say for eg: Ca
   String regex = createFilterRegex(filterTerm);
        mPattern = Pattern.compile(regex);

 private String createFilterRegex(String filterTerm) {
        filterTerm = Normalizer.normalize(filterTerm, Normalizer.Form.NFD);
       filterTerm = filterTerm.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
        return filterTerm;
    }

public Pattern getPattern() {
        return mPattern;
    }

In another class,

private SpannableStringBuilder createHighlightedString(String nodeText, int highlightColor) { //nodeText is the entire list displaying. 
        SpannableStringBuilder returnValue = new SpannableStringBuilder(nodeText);
        String lowercaseNodeText = nodeText;
        Matcher matcher = mFilter.getPattern().matcher((createFilterRegex(lowercaseNodeText)));
        while (matcher.find()) {
            returnValue.setSpan(new ForegroundColorSpan(highlightColor), matcher.start(0),
                    matcher.end(0), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }

        return returnValue;
    }

viewHolder.mTextView.setText(createHighlightedString((node.getText()), mHighlightColor));

But what i am getting output as,

If i type single alphabet o alone, it's highlighting but if i pass more than two alphabets say for eg: Ca, it's not highlighting and displaying. I couldn't figure out what mistake i am doing.

But if you look whatsapp. it has been achieved.

I typed Co, it's recognizing and highlighting accented characters in sentence.

enter image description here



from Given mixed accented and normal characters in string not working in java when searching

No comments:

Post a Comment