After trying a lot of libraries and method, I finally created my custom searchable spinner. The code I am attaching is at a very preliminary level which I will be updating as I do in my project. I am also writing the complete method of how to use it.
All Layouts
searchable_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/spinner_search_bar"
android:layout_width="match_parent"
android:layout_height="35dp"
android:hint="Select A Company"
android:cursorVisible="false"
android:background="@drawable/white_rect_fillet_border"
android:paddingHorizontal="10dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/ledger_list"
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="gone"
android:layout_marginHorizontal="10dp"/>
</LinearLayout>
</FrameLayout>
actity_or_frag_layout.xml remember to include this as last and align according to your parent layout.
<FrameLayout
android:id="@+id/spinner_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/white_rect_fillet_border"
app:layout_constraintTop_toTopOf="parent">
<include layout="@layout/searchable_spinner" />
</FrameLayout>
list_adapter_element.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical=5dp"
android:gravity="center_vertical"/>
</TableRow>
</TableLayout>
All Drawables
white_rect_fillet_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white"/>
<corners android:radius="5dp" />
<stroke android:color="@android:color/darker_gray"
android:width="1dp" />
</shape>
CustomListAdapter.java
public class CustomListAdapter extends
RecyclerView.Adapter<CustomListAdapter.ViewHolder> {
private final Activity context;
private Fragment fragment;
private ArrayList<LedgerListObject> ledgerlist; //replace LedgerListObject with your object or simply String everywhere in this code.
public CustomListAdapter(Activity context, Fragment fragment,
ArrayList<LedgerListObject> ledgerlist) {
this.context = context;
this.fragment = fragment;
this.ledgerlist = ledgerlist;
}
public void updateList(ArrayList<LedgerListObject> newList){
ledgerlist = newList;
notifyDataSetChanged();
}
@NonNull
@Override
public CustomListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_adapter_element, null, true);
return new ViewHolder(rowView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.txtTitle.setText(ledgerlist.get(position).LedgerName);
holder.txtTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyFragment.LedgerID = ledgerlist.get(position).LedgerID; //MyFragment can be replaced with the name of your activity or fragment
MyFragment.ledgerListView.setVisibility(View.GONE);
MyFragment.spinnerSearch.setText(ledgerlist.get(position).LedgerName);
MyFragment.spinnerSearch.setCursorVisible(false);
}
});
}
@Override
public int getItemCount() {
return ledgerlist.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView txtTitle;
ViewHolder(@NonNull View itemView) {
super(itemView);
txtTitle = (TextView) itemView.findViewById(R.id.txt);
}
}
}
MyFragment.java
public class MyFragment extends Fragment{
ArrayList<LedgerListObject> ledgerlist = new ArrayList<LedgerListObject>();
public static int LedgerID = 0;
CustomListAdapter ledgerAdapter;
FrameLayout spinnerFrame;
public static EditText spinnerSearch;
public static RecyclerView ledgerListView;
@SuppressLint("ClickableViewAccessibility")
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState){
View root = inflater.inflate(R.layout.fragment_ledger, container, false);
super.onCreate(savedInstanceState);
spinnerFrame = root.findViewById(R.id.spinner_frame);
spinnerSearch = root.findViewById(R.id.spinner_search_bar);
ledgerListView = root.findViewById(R.id.ledger_list);
spinnerSearch.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ledgerListView.setVisibility(View.VISIBLE);
spinnerSearch.setCursorVisible(true);
return false;
}
});
spinnerSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try{
filter(s.toString());
}catch (Exception e){e.printStackTrace();}
}
});
GridLayoutManager listgridLayoutManager = new GridLayoutManager(getContext(), 1,
RecyclerView.VERTICAL, false);
ledgerListView.setLayoutManager(listgridLayoutManager);
//todo: your method of adding objects to your ledgerlist
ledgerAdapter = new CustomListAdapter(getActivity(), LedgerFragment.this, ledgerlist);
ledgerListView.setAdapter(ledgerAdapter);
return root;
}
}
Try it and if there is any issue in this, please feel free to ask and I will resolve it