Set Selection in Spinner Through Another Spinner

In this tutorial, I am going to tell you about how to set selection on a searchable spinner. You can also use this trick in simple spinner components. You will learn how to select a spinner item from another spinner item.

Eg:- suppose you want to select a country and you automatically want its country code which is already stored in a database or in an array in a Spinner or vice versa.

Please visit my post to learn how to use searchable spinners in android.

Design a layout containing two spinners-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context="com.infy.seachablespinner.MainActivity">

  <com.toptoche.searchablespinnerlibrary.SearchableSpinner
      android:id="@+id/spcountries"

      android:layout_width="match_parent"
      android:layout_height="40sp">

  </com.toptoche.searchablespinnerlibrary.SearchableSpinner>
 <com.toptoche.searchablespinnerlibrary.SearchableSpinner
      android:id="@+id/spcounntrycode"
      android:layout_marginTop="10dp"

      android:layout_width="match_parent"
      android:layout_height="40sp">
  </com.toptoche.searchablespinnerlibrary.SearchableSpinner>

</LinearLayout>

Now In your mainActivity.java write:

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

import com.toptoche.searchablespinnerlibrary.SearchableSpinner;

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

public class MainActivity extends AppCompatActivity {
SearchableSpinner spcountries,spcountrycode;
ArrayList arraycountires,arraycountrycode;
ArrayAdapter adapter,adaptercode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spcountries=(SearchableSpinner) findViewById(R.id.spcountries);
        spcountrycode=(SearchableSpinner) findViewById(R.id.spcounntrycode);
        arraycountires=new ArrayList<String>(Arrays.asList("India", "Pakistan", "US", "China", "UK", "Itely", "France"));
        arraycountrycode=new ArrayList<String>(Arrays.asList("+91", "+92", "+80", "+94", "+90", "+34", "+11"));
        adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,arraycountires);
        adaptercode=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,arraycountrycode);
        spcountries.setAdapter(adapter);
        spcountries.setTitle("Select Countries");
        spcountries.setPositiveButton("Done");
        spcountrycode.setAdapter(adaptercode);
        spcountrycode.setTitle("Select Country Code");
        spcountrycode.setPositiveButton("Done");

        spcountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                long countryid=spcountries.getSelectedItemId();
//                String ccode= (String) arraycountrycode.get((int) countryid);
                spcountrycode.setSelection(spcountries.getSelectedItemPosition());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        spcountrycode.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                long countryid=spcountries.getSelectedItemId();
//                String ccode= (String) arraycountrycode.get((int) countryid);
                spcountries.setSelection(spcountrycode.getSelectedItemPosition());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}

Thank You!

 

 

Advertisements

Your Views And Question are Welcome Here!