Advertisements

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!

 

 

How useful was this post?

Do not forget to rate! this helps us to Improve.

Average rating / 5. Vote count:

Read More Like This..

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Advertisements

Safiya Akhtar

Safiya Akhtar is Android Application Developer in India. She is currently working as a public consultant for career and education too, besides blogging and freelancing. She believes that it is never too late or too early to start what you have always dreamed of.

Your Views And Question are Welcome Here!

Advertisements