how to use spinner in android studio
Hello Guys. Welcome to our new tutorial on how to use spinner in android using android studio. spinner in android is like the drop-down menu with multiple values from which the end-user can select only one value. please follow the code given below how to use spinner in the android studio.
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class Spinner extends AppCompatActivity { android.widget.Spinner spinner; List<String> list; ArrayAdapter<String> SpinnerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_spinner); spinner = (android.widget.Spinner) findViewById(R.id.spinner); list = new ArrayList<String>(); // List of Items list.add("Select your favorite city"); list.add("Mumbai"); list.add("Kolkata"); list.add("Delhi"); list.add("Goa"); list.add("Bengaluru"); list.add("Hyderabad"); list.add("Ahmedabad"); SpinnerAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, list){ public View getView(int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); ((android.widget.TextView) v).setTextColor(Color.parseColor("#009b9b")); return v; } public View getDropDownView(int position, View convertView, ViewGroup parent) { View v = super.getDropDownView(position, convertView, parent); v.setBackgroundColor(Color.parseColor("#9A9393")); ((android.widget.TextView) v).setTextColor(Color.parseColor("#ffffff")); return v; } }; SpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(SpinnerAdapter); // Set Adapter in the spinner spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { String state = parentView.getItemAtPosition(position).toString(); // selected item in the list ((TextView) findViewById(R.id.text)).setText(state); } @Override public void onNothingSelected(AdapterView<?> parentView) { } }); } } |
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<android.support.constraint.ConstraintLayout 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:background="#F2F2F2" tools:context=".Spinner"> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
Output:
Finally, run this project.
This is how Android spinner looks like.