Working with Spinner Android Studio Tutorial.

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.

                                                   

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

  1. package com.example.user.application2;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.AdapterView;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.Spinner;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. public class MainActivity extends AppCompatActivity {
  11. Spinner spinner1,spinner2;
  12. String[] Array={ "January","February","March","April","May","June","July","August","September","Octobe","November","December"};
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. spinner1 = (Spinner) findViewById(R.id.spinner1);
  18. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Array);
  19. spinner1.setAdapter(adapter);
  20. spinner2 = (Spinner) findViewById(R.id.spinner2);
  21. ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.days,android.R.layout.simple_spinner_item);
  22. spinner2.setAdapter(adapter1);
  23. spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  24. @Override
  25. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  26. Toast.makeText(MainActivity.this, ""+Array[position], Toast.LENGTH_SHORT).show();
  27. }
  28. @Override
  29. public void onNothingSelected(AdapterView<?> parent) {
  30. }
  31. });
  32. spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  33. @Override
  34. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  35. TextView tv = (TextView) view;
  36. Toast.makeText(MainActivity.this, ""+tv.getText(), Toast.LENGTH_SHORT).show();
  37. }
  38. @Override
  39. public void onNothingSelected(AdapterView<?> parent) {
  40. }
  41. });
  42. }
  43. }

activity.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="com.example.user.application2.MainActivity">
  9. <Spinner
  10. android:id="@+id/spinner1"
  11. android:layout_marginTop="40dp"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content">
  14. </Spinner>
  15. <Spinner
  16. android:id="@+id/spinner2"
  17. android:layout_marginTop="40dp"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content">
  20. </Spinner>
  21. </LinearLayout>

String.xml


  1. <resources>
  2. <string name="app_name">Application2</string>
  3. <string-array name="days">
  4. <item>Sunday</item>
  5. <item>Monday</item>
  6. <item>Tuesday</item>
  7. <item>Wednesday</item>
  8. <item>Thursday</item>
  9. <item>Friday</item>
  10. <item>Saturday</item>
  11. </string-array>
  12. </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. Working with Spinner Android Studio Tutorial. Reviewed by gcrt on March 20, 2018 Rating: 5

No comments:

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
Powered by Blogger.