Hello Readers! In this post, we are going to learn about how to use android spinner in any android application. We will also go through different attributes that are used to customise spinner widget.
Output
Getting Started
Spinner can be defined as below –
A spinner is a widget that have many options in dropdown, but only one option is displayed at a time. The displayed option is selected by the user. All the options in the spinner are provided using Adapter.
Note – You must provide an adapter with the some options so that user can pick one among them.
Attributes of Android Spinner Widget
Now, we will see different attributes that can be used to customise this widget.
Sr. | XML Attributes | Description |
---|---|---|
1 | android:dropDownHorizontalOffset | Use this attribute to set horizontal offset of the dropdown shown in the spinner. |
2 | android:dropDownSelector | This is used as list selector when spinnerMode is dropdown. |
3 | android:dropDownVerticalOffset | Use this attribute to set vertical offset of the dropdown shown in the spinner. |
4 | android:dropDownWidth | Use to set width of the dropdown when spinnerMode is dropdown. |
5 | android:gravity | Specifies the position of the currently selected item. |
6 | android:popupBackground | Use to set the background of the dropdown when spinnerMode is dropdown. |
7 | android:prompt | Prompt to show when the spinner’s dialog is shown. |
8 | android:spinnerMode | Use to set the display mode for spinner options. |
Attributes of Spinner are also inherited from AbsSpinner, ViewGroup and View. Some of the popular attributes of Spinner inherited from AbsSpinner are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:entries | Reference to an array resource that will populate the spinner. |
Some of the popular attributes of Spinner inherited from ViewGroup are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:addStatesFromChildren | Sets whether this viewGroup’s drawable state also include it’s children drawable states. |
2 | android:alwaysDrawnWithCache | Specifies whether viewGroup’s children will be drawn using their drawable cache or not. |
3 | android:animateLayoutChanges | Defines whether changes in layout (caused by adding and removing items) should cause a LayoutTransition to run. |
4 | android:animationCache | Specifies whether layout animations should create a drawing cache for their children. |
5 | android:clipChildren | Defines whether a child is limited to draw inside of its bounds or not. |
Some of the popular attributes of Spinner inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:alpha | Sets alpha of the view. |
2 | android:background | Sets background drawable of the view. |
3 | android:clickable | Sets whether view is clickable or not. |
4 | android:elevation | Sets base z-depth of the view. |
5 | android:id | Sets unique id of the view. Note – Id of the view must always be unique in an xml file. |
6 | android:padding | Sets padding of the view. |
7 | android:textAlignment | Sets alignment of the text. |
8 | android:visibility | Sets visibility(VISIBLE, INVISIBLE etc.) of the view. |
Note – Visit official documentation for more details about attributes.
Example of Android Spinner Widget
At First, we will create android application. Then, we will use android spinner in this application.
1. Creating New Project
Follow the steps below to create new project. Please ignore the steps if you have already created a new project.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as Spinner. Then, click next button. |
3. | Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button |
4. | Then, select Empty Activity => click next => click finish. |
5. | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail. |
Now, we will modify xml and java file to use android spinner in the project.
2. Modify Values folder
Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">Spinner</string> <string name="selected_item">Selected item:</string> </resources>
3. Use Android Spinner Widget in xml file
Open res/layout/activity_main.xml file. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:gravity="center"> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
In activity_main.xml file, we have added spinner widget. Now, we will access this widget in java file.
4. Access Spinner Widget in java file
Open src/main/java/com.tutorialwing.spinner/MainActivity.java file. Then, add below code into it.
package com.tutorialwing.spinner; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final String[] personNames = {"Rahul", "Jack", "Rajeev", "Aryan", "Rashmi", "Jaspreet", "Akbar"}; Spinner spinner = findViewById(R.id.spinner); if (spinner != null) { ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, personNames); spinner.setAdapter(arrayAdapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, getString(R.string.selected_item) + " " + personNames[position], Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } } }
In MainActivity.java file, we have accessed spinner widget. Then, we have created an adapter for the spinner and added it to spinner. Also, we are showing a toast message of selected item whenever any item is selected from dropdown of the spinner.
AndroidManifest.xml
Code inside src/main/AndroidManifest.xml file is as below –
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.tutorialwing.spinner" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
When you run the application, you will get output as shown above.
That’s the end of tutorial on Android Spinner Widget.
You must be logged in to post a comment.