Highlight search results in ListView - android

I have a ListView with Strings. With the below code I can highlight search results, but the user must type the words to search case sensitive. How can I implement a none - case sensitive highlighting of search results for example like the native Android Contact search?
Here is my code for Highlighting. I extend the ArrayAdapter and implement customized filter to get the string to search. In the getView method I check if my String in ListView contains the prefixString and highlight it.
public class HighlightListAdapter extends ArrayAdapter {
ArrayList<String> objects;
final Object mLock =new Object();
private ArrayList<String> mOriginalValues;
private ArrayFilter filter;
private String prefixString;
public AuthorsListAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
class ViewHolder{
TextView author;
}
public View getView(final int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
View v = convertView;
ViewHolder holder = null;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (v == null) {
holder = new ViewHolder();
v = inflater.inflate(R.layout.author_list_item, null);
holder.author =(TextView) v.findViewById(R.id.author_list_item_text);
v.setTag(holder);
}else{
holder = (ViewHolder) v.getTag();
}
final String author = objects.get(position);
if (author != null) {
holder.author.setText(author);
if(prefixString !=null && prefixString.length()>1){
String s = author;
**if(s.contains(prefixString)){
String rep = s.replace(prefixString, "<b><font color=#2825A6>"+ prefixString+ "</font></b>");
holder.author.setText(Html.fromHtml(rep));
}** // higlight
}
}
return v;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return objects.size();
}
#Override
public Filter getFilter() {
// TODO Auto-generated method stub
if(filter == null){
filter =new ArrayFilter();
}
return filter;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return this.objects.get(position);
}
private class ArrayFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (mLock) {
mOriginalValues = new ArrayList<String>(objects);
}
}
if (prefix == null || prefix.length() == 0) {
ArrayList<String> list;
synchronized (mLock) {
list = new ArrayList<String>(mOriginalValues);
}
results.values = list;
results.count = list.size();
} else {
**prefixString = prefix.toString();** // get string to search
ArrayList<String> values;
synchronized (mLock) {
values = new ArrayList<String>(mOriginalValues);
}
final int count = values.size();
final ArrayList<String> newValues = new ArrayList<String>();
for (int i = 0; i < count; i++) {
final String value = values.get(i);
final String valueText = value.toString().toLowerCase();
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString)) {
newValues.add(value);
} else {
final String[] words = valueText.split(" ");
final int wordCount = words.length;
// Start at index 0, in case valueText starts with space(s)
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString)) {
newValues.add(value);
break;
}
}
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
objects = (ArrayList<String>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
}

This what I use :
Every occurence is replaced (not only prefix)
Case and accent are ignored while searching but retained in the result.
It uses directly SpannableString, which you can use in setText(). I believe it's more efficient than using an intermediate html step.
.
public static CharSequence highlight(String search, String originalText) {
// ignore case and accents
// the same thing should have been done for the search text
String normalizedText = Normalizer.normalize(originalText, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();
int start = normalizedText.indexOf(search);
if (start < 0) {
// not found, nothing to to
return originalText;
} else {
// highlight each appearance in the original text
// while searching in normalized text
Spannable highlighted = new SpannableString(originalText);
while (start >= 0) {
int spanStart = Math.min(start, originalText.length());
int spanEnd = Math.min(start + search.length(), originalText.length());
highlighted.setSpan(new BackgroundColorSpan(<background_color>), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = normalizedText.indexOf(search, spanEnd);
}
return highlighted;
}
}

The accepted answer is nice. But you can do it by a single line of code. What I've done in my case to avoid the case sensitive issue is:
Spannable sb = new SpannableString(originalText);
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), originalText.toLowerCase().indexOf(query.toLowerCase()),
originalText.toLowerCase().indexOf(query.toLowerCase()) + query.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
result.setText(sb);
Hope it might help!
Note: Here 'query' is the part of the string that you want to highlight.

Simple & Advanced Search Highlighting Example [Case Insensitive Order]
1. Simple Search (Html):
public static void setSearchTextHighlightSimpleHtml(TextView textView, String fullText, String searchText) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
fullText = fullText.replaceAll("(?i)(" + searchText + ")", "<span style=\"background-color:#FCFF48;\"><b><big><font color='#a10901'>$1</font></big></b></span>");
textView.setText(Html.fromHtml(fullText, Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE);
} else {
fullText = fullText.replaceAll("(?i)(" + searchText + ")", "<b><big><font color='red'>$1</font></big></b>");
textView.setText(Html.fromHtml(fullText), TextView.BufferType.SPANNABLE);
}
} catch (Exception e) {
textView.setText(fullText);
}
}
2. Simple Search (Spannable):
public static void setSearchTextHighlightSimpleSpannable(TextView textView, String fullText, String searchText) {
// highlight search text
if (null != searchText && !searchText.isEmpty()) {
SpannableStringBuilder wordSpan = new SpannableStringBuilder(fullText);
Pattern p = Pattern.compile(searchText, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(fullText);
while (m.find()) {
int wordStart = m.start();
int wordEnd = m.end();
// Now highlight based on the word boundaries
ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{0xffa10901});
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null);
wordSpan.setSpan(highlightSpan, wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordSpan.setSpan(new BackgroundColorSpan(0xFFFCFF48), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordSpan.setSpan(new RelativeSizeSpan(1.25f), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setText(wordSpan, TextView.BufferType.SPANNABLE);
} else {
textView.setText(fullText);
}
}
3. Advanced Search (Spannable):
public static void setAdvancedSearch(TextView textView, String fullText, String searchText) {
if (searchText.length() == 0) {
textView.setText(fullText);
return;
}
final String searchBoundary = " \n()।.,;?-+!";
char[] boundaries = searchBoundary.toCharArray();
// highlight search text
if (isNotEquals(searchText, boundaries)) {
SpannableStringBuilder wordSpan = new SpannableStringBuilder(fullText);
Pattern p = Pattern.compile(searchText, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(fullText);
while (m.find()) {
int wordStart = m.start();
while (wordStart >= 0 && isNotEquals(fullText.charAt(wordStart), boundaries)) {
--wordStart;
}
wordStart = wordStart + 1;
int wordEnd = m.end();
while (wordEnd < fullText.length() && isNotEquals(fullText.charAt(wordEnd), boundaries)) {
++wordEnd;
}
setWordSpan(wordSpan, wordStart, wordEnd);
}
textView.setText(wordSpan, TextView.BufferType.SPANNABLE);
} else {
textView.setText(fullText);
}
}
private static boolean isNotEquals(char charAt, char[] boundaries) {
return isNotEquals(String.valueOf(charAt), boundaries);
}
private static boolean isNotEquals(String searchText, char[] boundaries) {
for (char boundary : boundaries) {
boolean equals = searchText.equals(String.valueOf(boundary));
if (equals) return false;
}
return true;
}
private static void setWordSpan(SpannableStringBuilder wordSpan, int wordStart, int wordEnd) {
// Now highlight based on the word boundaries
ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{0xffa10901});
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null);
wordSpan.setSpan(highlightSpan, wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordSpan.setSpan(new BackgroundColorSpan(0xFFFCFF48), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordSpan.setSpan(new RelativeSizeSpan(1.25f), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

First, your code
if(s.contains(prefixString)){
String rep = s.replace(prefixString, "<b><font color=#2825A6>"+ prefixString+ "</font></b>");
holder.author.setText(Html.fromHtml(rep));
}
is not good. You should use String.startsWith to check if the start of s equals to prefixString. Your actual code works, but it checks presence of prefixString in s, but doesn't care about its position.
For having case insensitive search, you can use String.toLowerCase or String.toUpperCase on both strings when checking presence of prefixString. Case will be ignored.
if(s.toLowerCase().startsWith(prefixString.toLowerCase())){
String rep = "<b><font color=#2825A6>" + prefixString + "</font></b>" + s.substring(prefixString.length());
holder.author.setText(Html.fromHtml(rep));
}

Related

Android Some device text watcher not working and some are working

I have working on mention edit text in below code i am extracting text after typing # but this code in working in some device and not in some device.
I have uploaded both video of working and not working in which showing also that which text is getting in text-watcher in red colour in autocomplete list.
This is working video
This is not working video
Any one help me for getting out this problem .This code is working in some devices but in some devices.
public class SocialMentionAutoComplete extends AppCompatMultiAutoCompleteTextView {
UserSearchChatAdapter userSearchChatAdapter;
ArrayMap<String, UserModel> map = new ArrayMap<>();
String formattedOfString = "#%s ";
Context context;
public SocialMentionAutoComplete(Context context) {
super(context);
initializeComponents(context);
}
public SocialMentionAutoComplete(Context context, AttributeSet attrs) {
super(context, attrs);
initializeComponents(context);
}
public SocialMentionAutoComplete(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initializeComponents(context);
}
private void initializeComponents(Context context) {
this.context = context;
addTextChangedListener(textWatcher);
setOnItemClickListener(onItemSelectedListener);
setTokenizer(new SpaceTokenizer());
}
AdapterView.OnItemClickListener onItemSelectedListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
UserModel mentionPerson = (UserModel) adapterView.getItemAtPosition(i);
map.put("#" + mentionPerson.username, mentionPerson);
}
};
/***
*This function returns the contents of the AppCompatMultiAutoCompleteTextView into my desired Format
*You can write your own function according to your needs
**/
public String getProcessedString() {
String s = getText().toString();
for (Map.Entry<String, UserModel> stringMentionPersonEntry : map.entrySet()) {
s = s.replace(stringMentionPersonEntry.getKey(), stringMentionPersonEntry.getValue().getFormattedValue());
}
return s;
}
/**
* This function will process the incoming text into mention format
* You have to implement the processing logic
*/
public void setMentioningText(String text) {
map.clear();
Pattern p = Pattern.compile("\\[([^]]+)]\\(([^ )]+)\\)");
Matcher m = p.matcher(text);
String finalDesc = text;
while (m.find()) {
UserModel mentionPerson = new UserModel();
String name = m.group(1);
String username = m.group(2);
//Processing Logic
finalDesc = finalDesc.replace("#[" + name + "](" + username + ")", "#" + username);
mentionPerson.name = name;
mentionPerson.username = username;
map.put("#" + username, mentionPerson);
}
int textColor = ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null);
Spannable spannable = new SpannableString(finalDesc);
for (Map.Entry<String, UserModel> stringMentionPersonEntry : map.entrySet()) {
int startIndex = finalDesc.indexOf(stringMentionPersonEntry.getKey());
int endIndex = startIndex + stringMentionPersonEntry.getKey().length();
spannable.setSpan(new ForegroundColorSpan(textColor), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
setText(spannable);
}
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence s, int start, int lengthBefore, int lengthAfter) {
try {
if (!s.toString().isEmpty() && s.length() > 1) {//start < s.length()
String name;
if (lengthAfter > lengthBefore) {
name = s.toString().substring(0, start + 1);
} else {
name = s.toString().substring(0, start);
}
if (name.contains("\n")) {
name = name.replaceAll("\n", "");
}
s = name;// this is main passing
int lastTokenIndex = name.lastIndexOf(" #");
int lastIndexOfSpace = name.lastIndexOf(" ");
int nextIndexOfSpace = name.indexOf(" ", start);
if (lastIndexOfSpace > 0 && lastTokenIndex < lastIndexOfSpace) {
String afterString = s.toString().substring(lastIndexOfSpace, s.length());
if (afterString.startsWith(" ") && !afterString.startsWith(" \n")) return;
}
if (lastTokenIndex < 0) {
if (!name.isEmpty() && name.length() >= 1 && name.startsWith("#")) {
lastTokenIndex = 1;
} else
return;
}
int tokenEnd = lastIndexOfSpace;
if (lastIndexOfSpace <= lastTokenIndex) {
tokenEnd = name.length();
if (nextIndexOfSpace != -1 && nextIndexOfSpace < tokenEnd) {
tokenEnd = nextIndexOfSpace;
}
}
if (lastTokenIndex >= 0) {
name = s.toString().substring(lastTokenIndex, tokenEnd).trim();
Pattern pattern = Pattern.compile("^(.+)\\s.+");
Matcher matcher = pattern.matcher(name);
if (!matcher.find()) {
// name = name.replace("#", "").trim();
if (name.equals("#")) {
// getPollsDetail("#");
} else if (!name.isEmpty()) {
getUsers(name);
}
}
}
} else if (!s.toString().isEmpty() && s.length() == 1 && s.toString().equals("#")) {
{
// getPollsDetail("#");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void afterTextChanged(Editable editable) {
}
};
/*
*This function returns results from the web server according to the user name
* I have used Retrofit for Api Communications
* */
public void getUsers(String name) {
AndroidNetworking.cancel("SearchUser");
String url = WebServiceUrl.SEARCH_USER + name.replace("#", "");
new WebTask().nameSearchAPI(context, url, new WebCompleteTaskNew() {
#Override
public void onComplete(String response, int taskcode, String callUrl) {
try {
JSONObject baseResponse = new JSONObject(response);
JSONObject jsonValue = baseResponse.getJSONObject("value");
if (taskcode == RequestCode.CODE_SEARCH_PEOPLE) {
List<UserModel> peopleArrayList = new Gson().fromJson(jsonValue.getJSONArray("data").toString(), new TypeToken<List<UserModel>>() {
}.getType());
userSearchChatAdapter = new UserSearchChatAdapter(getContext(), peopleArrayList, name);
SocialMentionAutoComplete.this.setAdapter(userSearchChatAdapter);
System.out.println("URLL::" + url + "::CLLLLL::" + callUrl);
if (callUrl.equals(url)) {
showDropDown();
}
// SocialMentionAutoComplete.this.showDropDown();
}
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onError(String message, int taskcode) {
}
});
}
public class SpaceTokenizer implements MultiAutoCompleteTextView.Tokenizer {
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != ' ') {
i--;
}
while (i < cursor && text.charAt(i) == ' ') {
i++;
}
return i;
}
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == ' ') {
return i;
} else {
i++;
}
}
return len;
}
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ') {
i--;
}
if (i > 0 && text.charAt(i - 1) == ' ') {
return text;
} else {
// Returns colored text for selected token
SpannableString sp = new SpannableString(String.format(formattedOfString, text));
int textColor = ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null);
sp.setSpan(new ForegroundColorSpan(textColor), 0, text.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sp;
}
}
}
}

Android Html.fromHtml(String) doesnt work

I want to set String with HTML tags effects. By using following method I am not able to do that. Its showing me normal text.
#SuppressWarnings("deprecation")
public static Spanned fromHtml(String html){
Spanned result;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(html);
}
return result;
}
I am passing following string to function;
vgdgffdgdgfdfgdfgdfgdfgdfg..........aererwerewrwerweryyiyuiuuyuuyiyiuy
hjjgjhghghgjhgjhgjgtttggtttghgggggg
But when I apply Html.fromHtml(html); its return following string
vgdgffdgdgfdfgdfgdfgdfgdfg.......... aererwerewrwerwer yyiyuiuuyuuyiyiuy hjjgjhghghgjhgjhgj gtttggtttghgggggg
I am running my app in emulator with API 23
Please provide some solution to handle HTML tags.
Android support some Html tags. you can see supported tags by android.
Supproted Html Tag by Android
Android not support ul and li tags. for that you have to handle tags like below
public class HtmlTagHandler implements Html.TagHandler {
boolean first = true;
String parent = null;
int index = 1;
private int mListItemCount = 0;
private Vector<String> mListParents = new Vector<String>();
#Override
public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {
if (tag.equals("ul") || tag.equals("ol") || tag.equals("dd")) {
if (opening) {
mListParents.add(tag);
} else mListParents.remove(tag);
mListItemCount = 0;
} else if (tag.equals("li") && !opening) {
handleListTag(output);
} else if (tag.equalsIgnoreCase("code")) {
if (opening) {
output.setSpan(new TypefaceSpan("monospace"), output.length(), output.length(), Spannable.SPAN_MARK_MARK);
} else {
Log.d("COde Tag", "Code tag encountered");
Object obj = getLast(output, TypefaceSpan.class);
int where = output.getSpanStart(obj);
output.setSpan(new TypefaceSpan("monospace"), where, output.length(), 0);
}
}
}
private Object getLast(Editable text, Class kind) {
Object[] objs = text.getSpans(0, text.length(), kind);
if (objs.length == 0) {
return null;
} else {
for (int i = objs.length; i > 0; i--) {
if (text.getSpanFlags(objs[i - 1]) == Spannable.SPAN_MARK_MARK) {
return objs[i - 1];
}
}
return null;
}
}
private void handleListTag(Editable output) {
if (mListParents.lastElement().equals("ul")) {
output.append("\n");
String[] split = output.toString().split("\n");
int lastIndex = split.length - 1;
int start = output.length() - split[lastIndex].length() - 1;
output.setSpan(new BulletSpan(15 * mListParents.size()), start, output.length(), 0);
} else if (mListParents.lastElement().equals("ol")) {
mListItemCount++;
output.append("\n");
String[] split = output.toString().split("\n");
int lastIndex = split.length - 1;
int start = output.length() - split[lastIndex].length() - 1;
output.insert(start, mListItemCount + ". ");
output.setSpan(new LeadingMarginSpan.Standard(15 * mListParents.size()), start, output.length(), 0);
}
}
}
Now you just have to create list item string with ul or li tag like below. In this method you just have to pass array of strings which you want to show as html list.
public static String getHtmlText(String[] s) {
String ulStart = "<ul>";
for (int i = 0; i < s.length; i++) {
ulStart += "<li>" + s[i] + "</li>";
}
ulStart += "</ul>";
return ulStart;
}
You can use like this:
textview.setText(Html.fromHtml(htmlString, null, new HtmlTagHandler()));

Is it possible to add animated emoji in android messaging

I am developing an emoji keyboard for android but don't know how to add a animated emoji in currentInputConnection of InputMethodService.
Edittext content= findVie......
sb = new SpannableStringBuilder();
String dummyText = "-";
sb.append(dummyText);
try {
sb.setSpan(anim = new AnimatedImageSpan(new AnimatedGifDrawable(
getAssets().open("54.gif"),
new AnimatedGifDrawable.UpdateListener() {
#Override
public void update() {
content.requestLayout();
content.invalidate();
}
})), sb.length() - dummyText.length(), sb.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
content.setText(sb);
here content is an editText view but I don't have any edit text view. All I have is currentInputConnection return by InputMethodService.getCurrentInputConnection().
If I understand you correctly, you're trying to send a "animated image" (sth like a gif) to EditText right? As far as I know, that may be impossible. If you have read the documentation of InputConnection, you should know that it doesn't provide any API to do this.
Actually I'm working on an Android IME project in which we implement an static Emoji input keyboard. All we do is to simply send the encoded bytes of emoji through InputConnection then the smiling face appears in the edit box.
If you have used WeChat, you may notice that its client has implemented a custom emoji keyboard(including static and dynamic content). That's because they know exactly what they're doing.
I am also trying to do the same thing. then i got my answer that .
If you want to implement within your app and sent message to your own app.
Then you can map each image pattern to some value.
////////
public class ViewsUtils {
private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();
static {
addPattern(emoticons, "\ud83d\udeb6", R.drawable.emot_d83ddeb6);
...
}
private static void addPattern(Map<Pattern, Integer> map, String smile,
int resource) {
map.put(Pattern.compile(Pattern.quote(smile)), resource);
}
public static boolean addSmiles(Context context, Spannable spannable) {
boolean hasChanges = false;
for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
Matcher matcher = entry.getKey().matcher(spannable);
while (matcher.find()) {
boolean set = true;
for (ImageSpan span : spannable.getSpans(matcher.start(),
matcher.end(), ImageSpan.class))
if (spannable.getSpanStart(span) >= matcher.start()
&& spannable.getSpanEnd(span) <= matcher.end())
spannable.removeSpan(span);
else {
set = false;
break;
}
if (set) {
hasChanges = true;
spannable.setSpan(new ImageSpan(context, entry.getValue()),
matcher.start(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return hasChanges;
}
public static void setText(TextView view, String text) {
if (null != view && null != text) {
Spanned spanned = Html.fromHtml(text);
SpannableString spannableString = SpannableString.valueOf(spanned);
addSmiles(view.getContext(), spannableString);
view.setText(spannableString);
}
}
public static void setText(View parent, int viewId, int resId) {
if (null != parent) {
String text = parent.getContext().getString(resId);
setText(parent, viewId, text);
}
}
public static void setText(View parent, int viewId, String text) {
if (null != parent) {
TextView view = (TextView) parent.findViewById(viewId);
if (null != view && null != text) {
Spanned spanned = Html.fromHtml(text);
SpannableString spannableString = SpannableString.valueOf(spanned);
addSmiles(view.getContext(), spannableString);
view.setText(spannableString);
}
}
}
public static void setText(View parent, int viewId, String text,
int visibility) {
if (null != parent) {
TextView view = (TextView) parent.findViewById(viewId);
if (null != view && null != text) {
Spanned spanned = Html.fromHtml(text);
SpannableString spannableString = SpannableString.valueOf(spanned);
addSmiles(view.getContext(), spannableString);
view.setText(spannableString);
view.setVisibility(visibility);
}
}
}
}
You just have to add map entries for the emote icons you want to handle in the static block. Using this class is pretty easy after that - just call some of the setText (or call addSmiles directly) methods. It would handle Html parsing too.

Highlight searched text in ListView items

I have a ListView and i am using a custom adapter to show data. Now i want to change searched text letter colour as in above screen shot.
Here is the code for SearchView
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar_menu_item, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
public boolean onQueryTextChange(String newText) {
// this is adapter that will be filtered
if (TextUtils.isEmpty(newText)){
lvCustomList.clearTextFilter();
}
else{
lvCustomList.setFilterText(newText.toString());
}
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
Thank you.
I assume that you have a custom Adapter with getCount() and getView() implemented and already filtering items, and you just need the bold part.
To achieve that, you need to use a SpannableString, which is basically text with markup attached. For example, a TextAppearanceSpan can be used to change typeface, font style, size, and color.
So, you should update your adapter's getView() to change the part where you use textView.setText() into something more or less like this:
String filter = ...;
String itemValue = ...;
int startPos = itemValue.toLowerCase(Locale.US).indexOf(filter.toLowerCase(Locale.US));
int endPos = startPos + filter.length();
if (startPos != -1) // This should always be true, just a sanity check
{
Spannable spannable = new SpannableString(itemValue);
ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
}
else
textView.setText(itemValue);
Android Search Highlight Example [Case Insensitive Order]
1. Search: [Highlight Specific Word]
public static SpannableStringBuilder highlightSearchText(SpannableStringBuilder fullText, String searchText) {
if (searchText.length() == 0) return fullText;
SpannableStringBuilder wordSpan = new SpannableStringBuilder(fullText);
Pattern p = Pattern.compile(searchText, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(fullText);
while (m.find()) {
int wordStart = m.start();
int wordEnd = m.end();
setWordSpan(wordSpan, wordStart, wordEnd);
}
return wordSpan;
}
2. Search: [Highlight Full Word]
public static SpannableStringBuilder highlightSearchText(SpannableStringBuilder fullText, String searchText) {
if (searchText.length() == 0) return fullText;
final String searchBoundary = " \n()।.,;?-+!";
char[] boundaries = searchBoundary.toCharArray();
// highlight search text
if (isNotEquals(searchText, boundaries)) {
SpannableStringBuilder wordSpan = new SpannableStringBuilder(fullText);
Pattern p = Pattern.compile(searchText, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(fullText);
while (m.find()) {
int wordStart = m.start();
while (wordStart >= 0 && isNotEquals(fullText.charAt(wordStart), boundaries)) {
--wordStart;
}
wordStart = wordStart + 1;
int wordEnd = m.end();
while (wordEnd < fullText.length() && isNotEquals(fullText.charAt(wordEnd), boundaries)) {
++wordEnd;
}
setWordSpan(wordSpan, wordStart, wordEnd);
}
return wordSpan;
} else {
return fullText;
}
}
private static boolean isNotEquals(String searchText, char[] boundaries) {
for (char boundary : boundaries) {
boolean equals = searchText.equals(String.valueOf(boundary));
if (equals) return false;
}
return true;
}
private static boolean isNotEquals(char charAt, char[] boundaries) {
for (char boundary : boundaries) {
boolean isEquals = charAt == boundary;
if (isEquals) return false;
}
return true;
}
Common Method:
private static void setWordSpan(SpannableStringBuilder wordSpan, int wordStart, int wordEnd) {
// Now highlight based on the word boundaries
ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{0xffa10901});
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null);
wordSpan.setSpan(highlightSpan, wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordSpan.setSpan(new BackgroundColorSpan(0xFFFCFF48), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordSpan.setSpan(new RelativeSizeSpan(1.25f), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
You can use the CodeView library to implement this feature, all you need is to create 2 variables in the adapter to store the pattern of search result and the color of highlighting,
private Pattern syntaxPattern;
private Color highlightColor = Color.MAGENTA;
and create a setter in the adapter for pattern and color
public void updateSyntaxPattern(Pattern pattern) {
syntaxPattern = pattern;
notifyDataSetChanged();
}
then in getView method for ArrayAdapter or ViewHolder in RecyclerAdapter you need to add this pattern to the CodeView instance and remove the old patterns (from the last search result)
if(syntaxPattern != null) {
codeView.resetSyntaxPatternList();
codeView.addSyntaxPattern(syntaxPattern, highlightColor);
}
Now in SearchView onQueryTextSubmit or onQueryTextChange, depend on when you want the highlighter to work you will choose one of them you need to make the pattern from the search result and set it to the adapter
Pattern pattern = Pattern.compile(query);
adapter.updateSyntaxPattern(pattern);
It will work exactly as you want

AutoCompleteTextView search input key in Adapter<T>

I was searching on google and stackover could not find the exact solution.
My problem is that, I have a ArrayList<String> adapter and it has
Gatewick London England
Ory Paris France
Heathrow London England
If user enters "Lon" into AutoCompleteTextView then I have to display line number 1 and 3. Because these have London string.
I tried this link and i pasted code here but it gives warning on line #57
String prefix = constraint.toString().toLowerCase();
PkmnAdapter
public class PkmnAdapter extends ArrayAdapter<String> {
private ArrayList<Pkmn> original;
private ArrayList<Pkmn> fitems;
private Filter filter;
public PkmnAdapter(Context context, int textViewResourceId,
ArrayList<Pkmn> items) {
super(context, textViewResourceId);
this.original = new ArrayList<Pkmn>(items);
this.fitems = new ArrayList<Pkmn>(items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Pkmn pkmn = fitems.get(position);
if (pkmn != null) {
TextView tt = (TextView) v.findViewById(R.id.RlabPName);
if (tt != null) {
tt.setText(pkmn.getNAME());
}
}
return v;
}
#Override
public Filter getFilter() {
if (filter == null)
filter = new PkmnNameFilter();
return filter;
}
private class PkmnNameFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if (prefix == null || prefix.length() == 0) {
ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
results.values = list;
results.count = list.size();
} else {
final ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
final ArrayList<Pkmn> nlist = new ArrayList<Pkmn>();
int count = list.size();
for (int i = 0; i < count; i++) {
final Pkmn pkmn = list.get(i);
final String value = pkmn.getNAME().toLowerCase();
if (value.startsWith(prefix)) {
nlist.add(pkmn);
}
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
fitems = (ArrayList<Pkmn>) results.values;
clear();
if (fitems != null) {
int count = fitems.size();
for (int i = 0; i < count; i++) {
Pkmn pkmn = (Pkmn) fitems.get(i);
fitems.add(pkmn);
}
}
}
}
}
MainActivity.java to put adapter
Pkmn[] item = new Pkmn[4];
item[0] = new Pkmn("Gatewick London England");
item[1] = new Pkmn("Ory Paris France");
item[2] = new Pkmn("Heathrow London England");
item[3] = new Pkmn("Ataturk Istanbul Turkey");
ArrayList<Pkmn> list = new ArrayList<Pkmn>(Arrays.asList(item));
MultiAutoCompleteTextView auto = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
PkmnAdapter adap = new PkmnAdapter(this,android.R.layout.simple_list_item_1, list);
First of all, if you enter "Lon" you should not check if the elements start with "Lon". Probably you need to switch the if statement to:
if (value.contains(prefix)) {
nlist.add(pkmn);
}
Before you perform any filtering in your performFiltering() method check if the constraint is null (Hint: use TextUtils class). If so, then return original data. Therefore you are avoiding NPE. You also need to pay attention to critical points where NPE can be thrown like this one:
if (prefix == null || prefix.length() == 0) { }
Cheers,

Categories

Resources