Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Android Spinner-Search Implementation

Hey, Developers!
This Tutorial will teach you how to implement a spinner with inbuilt search functionality. Here we have taken a small example where on choosing one item from the Spinner, a statement about the item selected will appear in Toast. TopToche Library is what we are using here to get the functionality. The implementation is quite easy and let’s follow the Tutorial now.

Why Searchable Spinner?

Well as we all know Spinners provide us a quick way to select a value from a set, one value at a time to be specific. Whenever we Touch a spinner, a dropdown menu is displayed and we can then make our choice.But sometimes our Layouts may be too congested or maybe the spinner is having a lot of items, selecting from which could be problematic. So in that cases, Searchable Spinners could be used. In this example when you touch a spinner, the traditional drop-down menu won’t be displayed but an Alert Builder kind of interface will be made available, with search functionalities. So you can easily select your choice, it saves time when you know your choice, so you quickly open it, search it, Spinner items get filtered, and select your option! That’s it!

The UI Will appear like this:

Creating Project – SearchableSpinner

  1. Open your Android Studio & create a new Project CustomSearch, we are using androidtutorial.com as our domain name but you may leave it as default and also we have taken Blank Activity for this project, you may go according to your requirements.
  2. The name for activity is by default MainActivity, and we have kept all the things by default and then clicked finish.

After the project is created and Gradle dependencies are resolved, you may start.

Add Gradle Dependencies

Build.gradle(Module: app )

dependencies {     
compile fileTree(dir: 'libs', include: ['*.jar'])   
testCompile 'junit:junit:4.12'     
compile 'com.android.support:appcompat-v7:25.1.1'     
compile 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1' 
} 

Creating Layout

activity_main.xml

We have assigned spinner id as spinner_search, all of the attributes are self-explanatory but still, you can feel free to ask anything in the comment section below.

strings.xml

SpinnerSearchSelect one ItemSamsungFoxconnAppleOppoNokiaLYFXiaomiHuaweiAsusLenovo

We need a list of String items for this.So we have made a string-array in strings.xml and we will assign this data later on to our Spinner in MainActivity.java

Creating Logic

MainActivity.java


package com.androidtutorialpoint.spinnersearch;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spinner = (Spinner) findViewById(R.id.spinner_search);
        // Creating ArrayAdapter using the string array and default spinner layout
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
                R.array.mobile_manufacturers, android.R.layout.simple_spinner_item);
        // Specify layout to be used when list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Applying the adapter to our spinner
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView> parent, View view, int position, long id) {
        String selectedItem = parent.getItemAtPosition(position).toString();
        switch (selectedItem) {
            case "Select one Item":
                break;
            case "Samsung":
                Toast.makeText(getApplicationContext(), selectedItem + " in Korean means 3 Stars!", Toast.LENGTH_SHORT).show();
                break;
            case "Foxconn":
                Toast.makeText(getApplicationContext(), selectedItem + " is world's largest contract electronics manufacturer", Toast.LENGTH_SHORT).show();
                break;
            case "Apple":
                Toast.makeText(getApplicationContext(), selectedItem + " was founded in 1976!", Toast.LENGTH_SHORT).show();
                break;
            case "Oppo":
                Toast.makeText(getApplicationContext(), selectedItem + " made first phone that can make 50MP photos", Toast.LENGTH_SHORT).show();
                break;
            case "Nokia":
                Toast.makeText(getApplicationContext(), selectedItem + " was founded 151 years ago!", Toast.LENGTH_SHORT).show();
                break;
            case "LYF":
                Toast.makeText(getApplicationContext(), selectedItem + " is an Indian Mobile Handset company", Toast.LENGTH_SHORT).show();
                break;
            case "Xiaomi":
                Toast.makeText(getApplicationContext(), selectedItem + " is world's 4th largest smartphone maker!", Toast.LENGTH_SHORT).show();
                break;
            case "Huawei":
                Toast.makeText(getApplicationContext(), selectedItem + " is largest telecommunications equipment manufacturer in the World! ", Toast.LENGTH_SHORT).show();
                break;
            case "Asus":
                Toast.makeText(getApplicationContext(), selectedItem + " name came from word PEGASUS", Toast.LENGTH_SHORT).show();
                break;
            case "Lenovo":
                Toast.makeText(getApplicationContext(), selectedItem + " is largest PC vendor by unit sales", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView> parent) {

    }
}

We are using ArrayAdapter object using string array and a default spinner layout. by using spinner.setAdapter(adapter) we will now apply the adapter to the spinner.
By using onItemSelectedListener we will apply a small logic by fetching Item name at a certain position (i.e position of the item which was clicked). We can’t use position, instead, we use item name in our switch case. After searching, the list will be filtered and hence the item’s position is now changed, so wrong output will be there, if we use position to raise Toast or do anything. Hence, we will use Item name which will be a string value to show appropriate Toasts. This approach is more appropriate for this implementation.

That’s it! Now you can run the app and see the changes yourself. In case of any queries you can comment your doubts below!

The post Android Spinner-Search Implementation appeared first on Android tutorials for hassle-free android development and programming.



This post first appeared on Android Tutorial Point, please read the originial post: here

Share the post

Android Spinner-Search Implementation

×

Subscribe to Android Tutorial Point

Get updates delivered right to your inbox!

Thank you for your subscription

×