How can we have searched characters colored when we use searchview in recyclerview?
After a hard effort I found the answer myself. I use a function like this:
fun colorsearch(a:String,charText: String):SpannableStringBuilder{
var l = 0
var b:ArrayList<Int>
b = ArrayList()
var w = 0
var i = 0
if (charText!=""){
label@ while (i < a.length) {
var j=0
while (j<charText.length){
Log.v("abc", j.toString())
if (i == a.length)
break@label
while ((a[i] != charText[j])) {
if (j != 0) {
continue@label
}
i++
if (i == a.length)
break@label
}
i++
j++
}
b.add(i)
w++
if (i == a.length)
break@label
}
}
val searchtitle = SpannableStringBuilder(a)
while (l < w) {
searchtitle.setSpan(
ForegroundColorSpan(Color.RED),
b[l] - charText.length, b[l],
Spanned.SPAN_EXCLUSIVE_INCLUSIVE
)
l++
}
return searchtitle
}
The "charText" in "a" will be Red.
Highlight filtered text in recyclerView
Add String filterPattern = "";
after ArrayList<POJO> cgpaArrayListcopy;
, then filterPattern can be accessed everywhere in the adapter.
To highlight filtered text, try the following codes:
@Override
public void onBindViewHolder(@NonNull adapter_cgpa.Viewholder holder, int position) {
POJO cgpa = cgpaArrayList.get(position);
if (!filterPattern.equals("")) {
String tmpCname = cgpa.getCname();
int startPos = tmpCname.toLowerCase(Locale.US).indexOf(filterPattern.toLowerCase(Locale.US));
int endPos = startPos + filterPattern.length();
if (startPos != -1) {
Spannable spannable = new SpannableString(tmpCname);
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);
holder.cname.setText(spannable);
} else {
holder.cname.setText(cgpa.getCname());
}
} else {
holder.cname.setText(cgpa.getCname());
}
holder.no_of_sems.setText(cgpa.getNo_of_sems());
holder.cgpa.setText(cgpa.getCgpa());
holder.percentage.setText(cgpa.getPercentage());
holder.schemec.setText(cgpa.getSchemec());
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<POJO> filteredList = new ArrayList<>();
if (TextUtils.isEmpty(constraint)) {
filterPattern = "";
filteredList.addAll(cgpaArrayListcopy);
} else {
filterPattern = constraint.toString().toLowerCase().trim();
for (POJO item : cgpaArrayListcopy) {
if (item.getCname().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
//} else {
// filteredList.remove(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
results.count = filteredList.size();
return results;
}
Hope that helps!
How to highlight filtered text in RecyclerView when using SearchView widget
change your adapter to
public class MyRecAdapter extends RecyclerView.Adapter<MyRecAdapter.VH> {
public List<Post> parkingList;
public Context context;
ArrayList<Post> mCountryModel;
String searchText;
public MyRecAdapter(List<Post> parkingList, Context context) {
this.parkingList = parkingList;
this.context = context;
}
@Override
public MyRecAdapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyRecAdapter.VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.mycardview, parent, false));
}
@Override
public void onBindViewHolder(MyRecAdapter.VH holder, int position) {
String title = parkingList.get(position).getPostTitle();
String desc = parkingList.get(position).getPostSubTitle();
holder.t1.setText(Html.fromHtml(title));
if(searchText.length()>0){
//color your text here
int index = desc.indexOf(searchText);
while(index>0){
SpannableStringBuilder sb = new SpannableStringBuilder(desc);
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158)); //specify color here
sb.setSpan(fcs, index, index+searchText.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
index = desc.indexOf(searchText,index+1);
}
holder.t2.setText(sb);
}else{
holder.t2.setText(Html.fromHtml(desc));
}
}
@Override
public int getItemCount() {
return parkingList.size();
}
public class VH extends RecyclerView.ViewHolder {
TextView t1, t2;
public VH(View view) {
super(view);
t1 = (TextView) view.findViewById(R.id.list_title);
t2 = (TextView) view.findViewById(R.id.list_desc);
}
}
public void setFilter(List<Post> countryModels,String searchText) {
mCountryModel = new ArrayList<>();
mCountryModel.addAll(countryModels);
this.searchText = searchText;
notifyDataSetChanged();
}
}
and set onQueryTextChange to
@Override
public boolean onQueryTextChange(String newText) {
final List<Post> filteredModelList = filter(list, newText);
if (filteredModelList.size() > 0) {
myRecAdapter.setFilter(filteredModelList,newText);
return true;
} else {
Toast.makeText(Chapter1.this, "Not Found", Toast.LENGTH_SHORT).show();
return false;
}
}
SearchView with Recycler View not working in Fragment
There is a few things wrong with this code.
If you would try and follow these steps (can be done in in onCreateView).
Init the recyclerview that is associated with this layout.
RecyclerView recyclerView = findViewById(R.id.recycler_view);Init the adapter that you would like to link with this recyclerview.
adapter = new MyAwesomeAdapter(myAwesomeCountryList);Add the adapter to the recyclerview.
recyclerView.setAdapter(adapter);Adding a layoutmanager to the recyclerview.
recyclerView.setLayoutManager(layoutManager)
If you have done that, you are quite close (hopefully it solves it but there are more things we can not see, such as how you set up your associated layout).
Related Topics
How to Make a Phone Call Programmatically
Implementing User Choice of Theme
How to Lock Orientation During Runtime
Display Badge on Top of Bottom Navigation Bar's Icon
Start Application Knowing Package Name
How to Display Icons in a Popupmenu
Converting Bitmap to Bytearray Android
"Realm Migration Needed", Exception in Android While Retrieving Values from Realm Db
Android - Controlling a Task with Timer and Timertask
Movecamera with Cameraupdatefactory.Newlatlngbounds Crashes
Check If Correct Google Play Service Available: "Unfortunately Application Has Stopped Working"
How to Build Signed APK from Android Studio for Flutter
Read & Writing Arrays of Parcelable Objects