From 513bc9a3a07e88c0b461dbd58e6480ae1dbe50ec Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sun, 4 Feb 2018 21:24:43 +0100 Subject: highlight searchword in messages --- .../java/de/pixart/messenger/ui/ConversationFragment.java | 3 ++- .../de/pixart/messenger/ui/adapter/MessageAdapter.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index b97dda837..0ceb93598 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -103,7 +103,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa private TextView messagehint_message; private RelativeLayout textsend; private RelativeLayout searchfield; - private EditText searchfield_input; + public EditText searchfield_input; private ImageButton searchUp; private ImageButton searchDown; private TextView snackbarMessage; @@ -1616,6 +1616,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa @Override public void onTextChanged(CharSequence s, int start, int before, int count) { + activity.refreshUi(); } }; diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index b005bf9e2..561bc8013 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -19,6 +19,7 @@ import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.format.DateUtils; +import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.text.style.RelativeSizeSpan; import android.text.style.StyleSpan; @@ -491,6 +492,19 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie final String nick = UIHelper.getMessageDisplayName(message); SpannableStringBuilder body = message.getMergedBody(); boolean hasMeCommand = message.hasMeCommand(); + String searchQuery = activity.mConversationFragment.searchfield_input.getText().toString().toLowerCase().trim(); + if (((!searchQuery.isEmpty() || !searchQuery.contains("")) && searchQuery.length() >= 3) && body.toString().toLowerCase().contains(searchQuery)) { + int ofe = body.toString().toLowerCase().indexOf(searchQuery, 0); + for (int ofs = 0; ofs < body.length() && ofe != -1; ofs = ofe + 1) { + ofe = body.toString().toLowerCase().indexOf(searchQuery, ofs); + if (ofe == -1) { + break; + } else { + body.setSpan(new StyleSpan(Typeface.BOLD), ofe, ofe + searchQuery.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + body.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + searchQuery.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + } + } if (hasMeCommand) { body = body.replace(0, Message.ME_COMMAND.length(), nick); } -- cgit v1.2.3