Basic Spinner Example

suggest change

Spinner It is a type of dropdown input. Firstly in layout

<Spinner
    android:id="@+id/spinner"     <!-- id to refer this spinner from JAVA-->
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
</Spinner>

Now Secondly populate values in spinner There are mainly two ways to populate values in spinner.

  1. From XML itself create a array.xml in values directory under res. Create this array
<string-array name="defaultValue">
    <item>--Select City Area--</item>
    <item>--Select City Area--</item>
    <item>--Select City Area--</item>
</string-array>

Now add this line in sppiner XML

android:entries="@array/defaultValue"
  1. You can also add values via JAVA

if you are using in activity cityArea = (Spinner) findViewById(R.id.cityArea); else if you are using in fragment

cityArea = (Spinner) findViewById(R.id.cityArea);

Now create a arrayList of Strings

ArrayList<String> area = new ArrayList<>();
//add values in area arrayList
cityArea.setAdapter(new ArrayAdapter<String>(context
                            , android.R.layout.simple_list_item_1, area));

This will look like

According to the device Android version it will render style

Following are some of the default themes

If an app does not explicitly request a theme in its manifest, Android System will determine the default theme based on the app’s targetSdkVersion to maintain the app’s original expectations:

Android SDK Version Default Theme
Version < 11 @android:style/Theme
Version between 11 and 13 @android:style/Theme.Holo
14 and higher @android:style/Theme.DeviceDefault

Spinner can be easily customized with the help of xml eg

android:background="@drawable/spinner_background"

 android:layout_margin="16dp"

 android:padding="16dp"

Create a custom background in XML and use it.

easily get the position and other details of the selected item in spinner

cityArea.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            areaNo = position;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

Change the text color of the selected item in spinner

This can be done in two ways in XML

<item android:state_activated="true" android:color="@color/red"/>

This will change the selected item color in the popup.

and from JAVA do this (in the setOnItemSelectedListener(…))

@Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                 ((TextView) parent.getChildAt(0)).setTextColor(0x00000000);
// similarly change `background color` etc.
            }

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Spinner:
* Basic Spinner Example

Table Of Contents
2 Gradle
5 Intent
17 Service
19 WebView
31 SQLite
35 Glide
37 Dialog
38 ACRA
44 Handler
53 Toast
63 Menu
65 Picasso
70 Volley
71 Widgets
78 Realm
90 Spinner
95 OkHttp
108 TextView
109 ListView
111 Loader
118 Xposed
119 Security
121 ImageView
123 Doze Mode
130 Drawables
131 Colors
134 Fresco
139 AdMob
145 Keyboard
146 Button
150 EditText
155 Vk SDK
163 ExoPlayer
169 XMPP
175 OpenCV
177 Threads
184 ORMLite
186 TabLayout
190 LruCache
192 Zip files
194 Fastlane
199 FileIO
202 Moshi
210 VideoView
216 Paint
218 ProGuard
226 CleverTap
228 ADB shell
229 Ping ICMP
230 AIDL
234 Context
240 JCodec
242 Okio
249 FuseView
254 Looper
261 Fastjson
263 Jackson
267 Smartcard