Menus

Tuesday, 4 June 2019

Android Drop Down List Tutorial



Spinner in Android application is equivalent of ComboBox

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.


You can add a spinner to your layout with the spinner.

For example

    <Spinner
        android:id="@+id/spnCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Spinner>



List of Items in Spinner

Open “res/values/strings.xml” file, define the list of items that will display in Spinner 

File : res/values/strings.xml
<resources>
    <string name="app_name">Layouts</string>

    <string name="prompt_country">Select a Country</string>

    <string-array name="country_list">
        <item>India</item>
        <item>United States</item>
        <item>Singapore</item>
        <item>Malaysia</item>>
        <item>France</item>
        <item>Italy</item>
        <item>New Zealand</item>
    </string-array>

</resources>


Spinner  selection items will be defined in xml file


<Spinner
        android:id="@+id/spnCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/country_list"
         >
</Spinner>

Other method :

<Spinner
        android:id="@+id/spnCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/prompt_country"
        android:entries="@array/country_list"
        android:spinnerMode="dialog"
         >

 </Spinner>

Spinner selection items will be defined in code

add code in onCreate method

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spnCountry = (Spinner) findViewById(R.id.spnCountry);

        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.country_list));
        myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spnCountry.setAdapter(myAdapter);


    }
}


Video





How to set selected item of Spinner programmatically in Android

Get spinner selected items text?

This example enplane value display on button click 

Java code
       Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                         Toast.makeText(TestActivity.this,String.valueOf(spnCountry.getSelectedItem()),Toast.LENGTH_LONG).show();
            }

        });

Example 2


Spinner spnCountry = (Spinner)findViewById(R.id.spnCountry);
String text = spnCountry.getSelectedItem().toString();


Handle spinner click event 


Spinner setOnItemSelectedListener


           spnCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(TestActivity.this,parent.getItemAtPosition(position) + " Selected",Toast.LENGTH_LONG).show();
            }

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

            }
        });


Set Key and Value in spinner

SpinnerCountry.java

package com.example.root.spinner;

/**
 * Created by root on 8/6/19.
 */
public class SpinnerCountry {

    public String id;
    public String country;

    public SpinnerCountry(String id,String country) {
        this.country = country;
        this.id = id;
    }

    @Override
    public String toString() {
        return country;
    }

}


MainActivity.java


package com.example.root.spinner;
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.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spnCountry = (Spinner) findViewById(R.id.spnCountry);

        ArrayList<SpinnerCountry> countryList = new ArrayList<SpinnerCountry>();
        //add coutries        countryList.add(new SpinnerCountry("1","India"));
        countryList.add(new SpinnerCountry("2","France"));
        countryList.add(new SpinnerCountry("3","United States"));

        //fill data in spinner
        ArrayAdapter<SpinnerCountry> spinnerAdapter = new ArrayAdapter<SpinnerCountry>(MainActivity.this,
                android.R.layout.simple_spinner_dropdown_item,countryList);
        spnCountry.setAdapter(spinnerAdapter);

// And this is how we can retrieve it upon item selection

        spnCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                SpinnerCountry spn = (SpinnerCountry) parent.getItemAtPosition(position);

                Toast.makeText(MainActivity.this,spn.id,Toast.LENGTH_LONG).show();
        
            }

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

            }
        });


    }
}


Video





1 comment:

  1. This content is very much helpful for me. Special Thanks and gratitude from my side.

    ReplyDelete