A Searchable Spinner in Android

What is A searchable Spinner?

Spinner is a drop-down list from which you can select one option. In Searchable Spinner, this list is combined with an edit text and when you write anything in edit text it will sort out the spinner list/items according to text you have written in the edit text.

Watch my video to learn how to use a searchable spinner And follow below steps to code.

You can use this Library on Searchable Spinner available on GitHub you just have to add this line to your app Gradle file:

compile 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'

 

Now got to your Activity resource file and paste the following code:

<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="com.infy.seachablespinner.MainActivity">

<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="@+id/spcountries"
app:hintText="Select Countries"
android:layout_width="match_parent"
android:layout_height="40sp">

</com.toptoche.searchablespinnerlibrary.SearchableSpinner>

</android.support.constraint.ConstraintLayout>

 

Now you can store some values in your array list in the code if you wanna try this then paste this code:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import com.toptoche.searchablespinnerlibrary.SearchableSpinner;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
SearchableSpinner spcountries;
ArrayList arraycountires;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spcountries=(SearchableSpinner) findViewById(R.id.spcountries);
arraycountires=new ArrayList<String>(Arrays.asList("India", "Pakistan", "US", "China", "UK", "Itely", "France"));
adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,arraycountires);
spcountries.setAdapter(adapter);
spcountries.setTitle("Select Countries");
spcountries.setPositiveButton("Done");

}
}

 

If you want to add the data in spinner through database then write following:

To connect your application to SQL server database you can refer to this post.

In

@Override
protected Object doInBackground(Object[] objects) {

String query="Select * from mycountries";

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
arraycountires.add(rs.getString("country"));
}

}

 

Tips:

  1. You can use these methods to set title and hint for your searchable spinner.
  2. Make simple and fast executing queries to fil the spinner fast through the database and also it gives a better experience to users.
  3. You can use searchable spinners where you want to search items from a list.

[amazon_link asins=’007160930X,1931945772,0836235908,B01AZY10OU,1503357929,0882821504′ template=’ProductCarousel’ store=’lifenews525-21′ marketplace=’IN’ link_id=’ c19da54f-e98b-11e8-809d-2b66d81b3ede’]

Advertisements

4 comments

Your Views And Question are Welcome Here!