Android Searchable Spinner



Libraries

implementation 'com.mcxiaoke.volley:library:1.0.19'
implementation 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'


SearchableSpinnerActivity.java

 package hemanth.smartkit.com.Activity;  
 import android.app.ProgressDialog;  
 import android.os.AsyncTask;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.AdapterView;  
 import android.widget.ArrayAdapter;  
 import android.widget.Spinner;  
 import android.widget.TextView;  
 import com.android.volley.Request;  
 import com.android.volley.Response;  
 import com.android.volley.VolleyError;  
 import com.android.volley.toolbox.StringRequest;  
 import com.android.volley.toolbox.Volley;  
 import org.json.JSONArray;  
 import org.json.JSONException;  
 import org.json.JSONObject;  
 import java.util.ArrayList;  
 import hemanth.smartkit.com.Adapter.SourceOfFundsSpinnerAdapter;  
 import hemanth.smartkit.com.Bean.SourceOfFundsSpinnerBean;  
 import hemanth.smartkit.com.R;  
 public class SearchableSpinnerActivity extends AppCompatActivity {  
   SourceOfFundsSpinnerAdapter sourceOfFundsSpinnerAdapter;  
   Spinner SourceofFundSpinner;  
   private ProgressDialog mprocessingdialog;  
   ArrayList<String> SourceOfFundsSpinnerArray = new ArrayList<String>();  
   ArrayList<SourceOfFundsSpinnerBean> SourceOfFundsSpinnerArrayCode = new ArrayList<SourceOfFundsSpinnerBean>();  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_searchable_spinner);  
     SourceofFundSpinner = (Spinner) findViewById(R.id.SourceofFundSpinner);  
     new SearchableSpinnerActivity.GetSourceOfFundsSpinnerAsync().execute();  
   }  
   private class GetSourceOfFundsSpinnerAsync extends AsyncTask<Void, Void, Void> {  
     @Override  
     protected void onPreExecute() {  
       // TODO Auto-generated method stub  
       super.onPreExecute();  
       mprocessingdialog = new ProgressDialog(SearchableSpinnerActivity.this);  
       mprocessingdialog.setTitle("Please Wait..");  
       mprocessingdialog.setMessage("Loading");  
       mprocessingdialog.setIndeterminate(true);  
       mprocessingdialog.show();  
     }  
     @Override  
     protected Void doInBackground(Void... arg0) {  
       StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://54.244.96.209:8080/Crosspaydevelopment/upload/getSourceofFunds",  
           new Response.Listener<String>() {  
             @Override  
             public void onResponse(String response) {  
 //              Log.e("rlog", "response" + response);  
               try {  
                 JSONObject jsonObj = new JSONObject(response);  
                 JSONArray jsonArray = jsonObj.getJSONArray("data");  
                 for (int i = 0; i < jsonArray.length(); i++) {  
                   SourceOfFundsSpinnerBean item = new SourceOfFundsSpinnerBean();  
                   JSONObject c = jsonArray.getJSONObject(i);  
                   item.setSof_code(c.getString("sof_code"));  
                   item.setSof_description(c.getString("sof_description"));  
                   SourceOfFundsSpinnerArray.add(c.getString("sof_description"));  
                   SourceOfFundsSpinnerArrayCode.add(item);  
                 }  
                 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SearchableSpinnerActivity.this,  
                     android.R.layout.simple_spinner_item, SourceOfFundsSpinnerArray);  
                 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
                 dataAdapter.notifyDataSetChanged();  
                 SourceofFundSpinner.setAdapter(dataAdapter);  
 //                Collections.sort(SourceOfFundsSpinnerArrayCode, new Comparator<SourceOfFundsSpinnerBean>() {  
 //                  public int compare(SourceOfFundsSpinnerBean o1, SourceOfFundsSpinnerBean o2) {  
 //                    if (o1.getSof_description() == null || o2.getSof_description() == null) {  
 //                      return 0;  
 //                    }  
 //  
 //                    return o1.getSof_description().compareToIgnoreCase(o2.getSof_description());  
 //                  }  
 //                });  
 //                Collections.sort(SourceOfFundsSpinnerArray, new Comparator<String>() {  
 //                  @Override  
 //                  public int compare(String text1, String text2) {  
 //                    return text1.compareToIgnoreCase(text2);  
 //                  }  
 //                });  
                 SourceofFundSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
                   @Override  
                   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
 //                    SourceOfFundsSpinnerBean c = SourceOfFundsSpinnerArrayCode.get(position);  
 //                    StringSourceofFundCode = c.getSof_code();  
 //                    Toast.makeText(getApplicationContext(),StringSourceofFundCode,Toast.LENGTH_LONG).show();  
 //                    ((TextView) SourceofFundSpinner.getSelectedView()).setTextColor(getResources().getColor(R.color.white));  
                   }  
                   @Override  
                   public void onNothingSelected(AdapterView<?> parent) {  
 //                    StringSourceofFundCode = "-1";  
                   }  
                 });  
 //                sourceOfFundsSpinnerAdapter = new SourceOfFundsSpinnerAdapter(getApplicationContext(), SourceOfFundsSpinnerArray);  
 //                SourceofFundSpinner.setAdapter(sourceOfFundsSpinnerAdapter);  
 //                SourceofFundSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
 //                  @Override  
 //                  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
 //                    SourceOfFundsSpinnerBean c = SourceOfFundsSpinnerArrayCode.get(position);  
 //                    StringSourceofFundCode = c.getSof_code();  
 //                    Toast.makeText(getApplicationContext(),StringSourceofFundCode,Toast.LENGTH_LONG).show();  
 //                    ((TextView) SourceofFundSpinner.getSelectedView()).setTextColor(getResources().getColor(R.color.white));  
 //                  }  
 //  
 //                  @Override  
 //                  public void onNothingSelected(AdapterView<?> parent) {  
 //  
 //                  }  
 //                });  
               } catch (JSONException e) {  
                 e.printStackTrace();  
               }  
             }  
           }, new Response.ErrorListener() {  
         @Override  
         public void onErrorResponse(VolleyError error) {  
         }  
       });  
       Volley.newRequestQueue(SearchableSpinnerActivity.this).add(stringRequest);  
       return null;  
     }  
     @Override  
     protected void onPostExecute(Void result) {  
       // TODO Auto-generated method stub  
       super.onPostExecute(result);  
       mprocessingdialog.dismiss();  
     }  
   }  
 }  


SourceOfFundsSpinnerAdapter.java

 package hemanth.smartkit.com.Adapter;  
 import android.content.Context;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.BaseAdapter;  
 import android.widget.TextView;  
 import java.util.ArrayList;  
 import hemanth.smartkit.com.Bean.SourceOfFundsSpinnerBean;  
 import hemanth.smartkit.com.R;  
 /**  
  * Created by heman on 15-11-2017.  
  */  
 public class SourceOfFundsSpinnerAdapter extends BaseAdapter {  
   Context context;  
   ArrayList<SourceOfFundsSpinnerBean> data = new ArrayList<SourceOfFundsSpinnerBean>();  
   LayoutInflater inflter;  
   TextView txt;  
   public SourceOfFundsSpinnerAdapter(Context applicationContext, ArrayList<SourceOfFundsSpinnerBean> data) {  
     this.context = applicationContext;  
     this.data = data;  
     inflter = (LayoutInflater.from(applicationContext));  
   }  
   public int getCount() {  
     return data.size();  
   }  
   @Override  
   public Object getItem(int i) {  
     return null;  
   }  
   @Override  
   public long getItemId(int i) {  
     return 0;  
   }  
   @Override  
   public View getDropDownView(final int i, View view, final ViewGroup parent) {  
     view = inflter.inflate(R.layout.register_country_spinner_list_dropdown, null);  
     txt = (TextView) view.findViewById(R.id.txt);  
     txt.setText(data.get(i).getSof_description());  
     return view;  
   }  
   @Override  
   public View getView(final int i, View view, ViewGroup viewGroup) {  
     view = inflter.inflate(R.layout.register_country_spinner_list_single, null);  
     TextView txt = (TextView) view.findViewById(R.id.txt);  
     txt.setText(data.get(i).getSof_description());  
     return view;  
   }  
 } 



SourceOfFundsSpinnerBean.java

package hemanth.smartkit.com.Bean;  
 /**  
  * Created by heman on 15-11-2017.  
  */  
 public class SourceOfFundsSpinnerBean {  
   String sof_code;  
   String sof_description;  
   public String getSof_code() {  
     return sof_code;  
   }  
   public void setSof_code(String sof_code) {  
     this.sof_code = sof_code;  
   }  
   public String getSof_description() {  
     return sof_description;  
   }  
   public void setSof_description(String sof_description) {  
     this.sof_description = sof_description;  
   }  
 }  


 activity_searchable_spinner.xml


 <?xml version="1.0" encoding="utf-8"?>  
 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context=".Activity.SearchableSpinnerActivity">  
   <com.toptoche.searchablespinnerlibrary.SearchableSpinner  
     android:id="@+id/SourceofFundSpinner"  
     style="@style/Widget.AppCompat.Spinner.Underlined"  
     android:layout_width="match_parent"  
     android:layout_height="50dp"  
     android:layout_marginLeft="20dp"  
     android:layout_marginRight="20dp"  
     android:spinnerMode="dialog"  
     app:hintText="Choose Source of Fund" />  
 </android.support.constraint.ConstraintLayout>  



register_country_spinner_list_single.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="horizontal"  
   >  
   <TableRow  
     android:id="@+id/relative"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:orientation="horizontal" >  
     <TextView  
       android:id="@+id/txt"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_margin="5dp"  
       android:text="GDP"  
       android:textAllCaps="true"  
       android:textSize="15dp" />  
   </TableRow>  
 </LinearLayout>  


register_country_spinner_list_dropdown.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:baselineAligned="false"  
   android:descendantFocusability="blocksDescendants"  
   android:orientation="horizontal">  
   <TableRow  
     android:id="@+id/relative"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:orientation="vertical">  
     <TableRow  
       android:layout_width="match_parent"  
       android:layout_height="match_parent"  
       android:layout_gravity="center"  
       android:layout_margin="5dp"  
       android:orientation="vertical">  
       <LinearLayout  
         android:id="@+id/L1"  
         android:layout_width="match_parent"  
         android:layout_height="match_parent"  
         android:layout_gravity="center"  
         android:orientation="horizontal">  
         <LinearLayout  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_gravity="center"  
           android:layout_weight="1"  
           android:orientation="vertical">  
           <TextView  
             android:id="@+id/txt"  
             android:layout_width="wrap_content"  
             android:layout_height="wrap_content"  
             android:layout_margin="10dp"  
             android:textAllCaps="true"  
             android:textSize="15dp" />  
         </LinearLayout>  
       </LinearLayout>  
     </TableRow>  
   </TableRow>  
 </LinearLayout>