Android Searchable Spinner Example

Hiii Everyone in this example I am sharing how to use Serch view in Spinner.  In Android used a dropdown list with Spinner to show a list of data in dropdown throw API. So In this tutorial, I am implementing and Spiner with search View.

In this example implement Searchablespinner GitHub Library to use an easy way for search view in a spinner.

Android Searchable Spinner Example

https://www.youtube.com/watch?v=xSNkG3P_tNo

Step 1: Add a Searchable Spinner Library in your build.gradle.  

compile 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'
compile 'com.android.volley:volley:1.0.0'

Step 2:  Open Your activity.xml file and add these.

In your XML file used searchablespinner view in and create a UI.

<com.toptoche.searchablespinnerlibrary.SearchableSpinner
    android:id="@+id/spinnerPart"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:background="@drawable/editbox"
    android:layout_height="40dp" />

Step 3: Open Your Activity Java file and Implement it.

In your java file add spinner and implement it and Call API for getting data using volley library. and set data in Spinner

public class Main extends AppCompatActivity{

private Spinner PartSpinner;
List<String> PartName;
List<String> PartId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
  
   PartSpinner=(Spinner)findViewById(R.id.spinnerPart);
   PartName=new ArrayList<>();
   PartId=new ArrayList<>();
  }

     private void PartList() {
        final ProgressDialog loading = new ProgressDialog(CompleteCondition.this);
        loading.setMessage("Please Wait...");
        loading.show();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, ConfiURL.Parts_List_URl,
        new Response.Listener<String>() {
             
           @Override
            public void onResponse(String response) {
                  try {
                   Log.d("JSON", response);
                    loading.dismiss();
                     JSONObject eventObject = new JSONObject(response);
                     String error_status=eventObject.getString("error");
                      if(error_status.equals("true")){
                       String error_msg=eventObject.getString("msg");
                       Toast.makeText(CompleteCondition.this, error_msg, Toast.LENGTH_SHORT).show();
                         }
                          else{
                          Resouces(eventObject);
                           }
                            } catch (Exception e) {
                              Log.d("Tag",e.getMessage());

                               }
                                }
                              },

                             new Response.ErrorListener() {
                                 @Override
                                public void onErrorResponse(VolleyError error) {
                                loading.dismiss();
                                Toast.makeText(CompleteCondition.this,error.toString(), Toast.LENGTH_LONG ).show();

                                 }
                               }){
                         @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                          Map<String,String> map = new HashMap<String,String>();
                            return map;
                                }
                              };

                         RequestQueue requestQueue = Volley.newRequestQueue(this);
                         requestQueue.add(stringRequest);
                       }
                     /*hare to pares the json response in list and show to spener*/
                       public void Resouces(JSONObject Ward) {
                        String Resourc_Name;
                        String ResourceID;
                         PartName.clear();
                         PartId.clear();
                         PartName.add("Select Part");
                         PartId.add("Select Part");
                        try {
                         JSONArray projectNameArray = Ward.getJSONArray("Available Parts");
                          for (int i = 0; i <= projectNameArray.length(); i++) {
                           JSONObject obj = projectNameArray.getJSONObject(i);
                            Resourc_Name = obj.getString("part_name");
                              ResourceID = obj.getString("part_id");
                              Log.d("name", Resourc_Name);
                              PartName.add(Resourc_Name);
                              PartId.add(ResourceID);

                              //Log.d("Dropdown",Dropdown.get(i));
                              }
                              } catch (Exception e) {
                              e.printStackTrace();
                              }
                            ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, PartName);
                             PartSpinner.setAdapter(adapter);
                              PartSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                         @Override
                        public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
                       // On selecting a spinner item
                       ((TextView) adapter.getChildAt(0)).setTextColor(Color.BLACK);
                        Part_Id = PartId.get(position);

                         }

                           @Override
                       public void onNothingSelected(AdapterView<?> arg0) {
                   // TODO Auto-generated method stub
                    }
                  });
                 }
                }

 

 

You may also like...