Skip to content

Commit

Permalink
Merge pull request #12 from Xtr126/main
Browse files Browse the repository at this point in the history
Highlight text when performing search
  • Loading branch information
axel358 authored Nov 19, 2022
2 parents a2463da + 83a9127 commit b4a9395
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions app/src/main/java/cu/axel/smartdock/adapters/AppAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import android.content.Context;
import android.preference.PreferenceManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -11,6 +14,7 @@
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
import android.graphics.Typeface;
import cu.axel.smartdock.R;
import cu.axel.smartdock.icons.IconParserUtilities;
import cu.axel.smartdock.models.App;
Expand All @@ -28,6 +32,7 @@ public class AppAdapter extends ArrayAdapter<App> {
private boolean iconTheming, tabletMode;
private IconParserUtilities iconParserUtilities;
private AppRightClickListener rightClickListener;
private String query;

public AppAdapter(Context context, AppRightClickListener listener, ArrayList<App> apps) {
super(context, R.layout.app_entry, apps);
Expand Down Expand Up @@ -68,8 +73,21 @@ public View getView(int position, View convertView, ViewGroup parent) {
holder = (ViewHolder) convertView.getTag();

final App app = apps.get(position);
holder.nameTv.setText(app.getName());

String name = app.getName();

if (query != null) {
int spanStart = name.toLowerCase().indexOf(query.toLowerCase());
int spanEnd = spanStart + query.length();
if (spanStart != -1) {
SpannableString spannable = new SpannableString(name);
spannable.setSpan(new StyleSpan(Typeface.BOLD), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.nameTv.setText(spannable);
} else {
holder.nameTv.setText(name);
}
} else {
holder.nameTv.setText(name);
}

if (iconTheming)
holder.iconIv.setImageDrawable(iconParserUtilities.getPackageThemedIcon(app.getPackageName()));
Expand Down Expand Up @@ -114,6 +132,7 @@ private class AppFilter extends Filter {
protected Filter.FilterResults performFiltering(CharSequence p1) {
FilterResults results = new FilterResults();
String query = p1.toString().trim().toLowerCase();
AppAdapter.this.query = query;
if (query.length() > 1) {
ArrayList<App> filteredResults = new ArrayList<App>();

Expand Down

0 comments on commit b4a9395

Please sign in to comment.