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.

Hope this post is helpful,for more useful post like and share with your friends, we also have Youtube channel for more details check out the video below, do like share and subscribe to our channel it will boost our energy and help to improve our work, Have Nice Day.

Here we have two spinners we will read spinner element using Array another with String.xml.Let's see the code for it.
MainActivity.java
- package com.example.user.application2;
- 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.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- Spinner spinner1,spinner2;
- String[] Array={ "January","February","March","April","May","June","July","August","September","Octobe","November","December"};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- spinner1 = (Spinner) findViewById(R.id.spinner1);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Array);
- spinner1.setAdapter(adapter);
- spinner2 = (Spinner) findViewById(R.id.spinner2);
- ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.days,android.R.layout.simple_spinner_item);
- spinner2.setAdapter(adapter1);
- spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- Toast.makeText(MainActivity.this, ""+Array[position], Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- TextView tv = (TextView) view;
- Toast.makeText(MainActivity.this, ""+tv.getText(), Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- }
- }
activity.xml
- <?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.example.user.application2.MainActivity">
- <Spinner
- android:id="@+id/spinner1"
- android:layout_marginTop="40dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- </Spinner>
- <Spinner
- android:id="@+id/spinner2"
- android:layout_marginTop="40dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- </Spinner>
- </LinearLayout>
String.xml
- <resources>
- <string name="app_name">Application2</string>
- <string-array name="days">
- <item>Sunday</item>
- <item>Monday</item>
- <item>Tuesday</item>
- <item>Wednesday</item>
- <item>Thursday</item>
- <item>Friday</item>
- <item>Saturday</item>
- </string-array>
- </resources>
for more detail check out the video below.
Hope this post is helpful,for more useful post like and share with your friends, we also have Youtube channel for more details check out the video below, do like share and subscribe to our channel it will boost our energy and help to improve our work, Have Nice Day.
Working with Spinner Android Studio Tutorial.
Reviewed by gcrt
on
March 20, 2018
Rating:

No comments: