Android Read Index of Spinner Item Position
Android Spinner Tutorial With Case
Hullo Readers! In this mail, we are going to larn well-nigh how to use android spinner in whatsoever android awarding. We will besides become through different attributes that are used to customise spinner widget.
Output
Tutorialwing Android Spinner Output
Getting Started
Spinner tin can be defined as below –
A spinner is a widget that accept many options in dropdown, but only ane pick 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 amongst them.
Attributes of Android Spinner Widget
Now, we will see unlike attributes that tin be used to customise this widget.
| Sr. | XML Attributes | Description |
|---|---|---|
| 1 | android:dropDownHorizontalOffset | Use this attribute to set up horizontal starting time of the dropdown shown in the spinner. |
| 2 | android:dropDownSelector | This is used as list selector when spinnerMode is dropdown. |
| 3 | android:dropDownVerticalOffset | Apply this attribute to prepare vertical starting time of the dropdown shown in the spinner. |
| 4 | android:dropDownWidth | Utilize to set width of the dropdown when spinnerMode is dropdown. |
| five | android:gravity | Specifies the position of the currently selected particular. |
| six | android:popupBackground | Use to set the groundwork 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 volition populate the spinner. |
Some of the popular attributes of Spinner inherited from ViewGroup are –
| Sr. | XML Attributes | Clarification |
|---|---|---|
| 1 | android:addStatesFromChildren | Sets whether this viewGroup's drawable state also include it's children drawable states. |
| ii | android:alwaysDrawnWithCache | Specifies whether viewGroup'due south children volition be fatigued 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 kid is limited to draw inside of its premises or not. |
Some of the popular attributes of Spinner inherited from View are –
| Sr. | XML Attributes | Description |
|---|---|---|
| 1 | android:blastoff | Sets blastoff of the view. |
| 2 | android:groundwork | Sets groundwork drawable of the view. |
| 3 | android:clickable | Sets whether view is clickable or not. |
| 4 | android:summit | Sets base z-depth of the view. |
| five | android:id | Sets unique id of the view. Annotation – Id of the view must always exist 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 than details about attributes.
Case 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 y'all have already created a new project.
| Stride | Description |
|---|---|
| 1. | Open Android Studio. |
| 2. | Go to File => New => New Projection. Write application name equally Spinner. Then, click next button. |
| three. | Select minimum SDK yous need. Notwithstanding, nosotros take selected 17 equally minimum SDK. Then, click next button |
| 4. | Then, select Empty Activity => click next => click finish. |
| 5. | If you lot have followed higher up process correctly, yous volition get a newly created project successfully. However, you tin besides visit postal service to create a new project to know steps in detail. |
Now, we will alter xml and coffee file to apply android spinner in the projection.
2. Change Values folder
Open res/values/strings.xml file. So, add below code into information technology.
<resources> <string name="app_name">Spinner</string> <cord name="selected_item">Selected item:</string> </resources>
3. Utilise Android Spinner Widget in xml file
Open res/layout/activity_main.xml file. Then, add beneath code into it.
<?xml version="i.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="centre"> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
In activity_main.xml file, nosotros have added spinner widget. Now, we will admission this widget in java file.
4. Admission Spinner Widget in java file
Open src/primary/coffee/com.tutorialwing.spinner/MainActivity.java file. And then, add below code into it.
package com.tutorialwing.spinner; import android.bone.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 grade MainActivity extends AppCompatActivity { @Override protected void onCreate(Parcel 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 != zilch) { 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).testify(); } @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, nosotros are showing a toast bulletin of selected item whenever whatsoever item is selected from dropdown of the spinner.
AndroidManifest.xml
Code within src/primary/AndroidManifest.xml file is as below –
<?xml version="one.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="truthful" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.activeness.Chief"/> <category android:proper noun="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
When you run the application, you will get output as shown in a higher place.
That's the terminate of tutorial on Android Spinner Widget.
Source: https://tutorialwing.com/android-spinner-tutorial-with-example/
0 Response to "Android Read Index of Spinner Item Position"
Post a Comment