• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar

DhamenORM

  • Home
  • Download
    • Android SQLite Code Generator
    • Dhamen Code Snippet Manager
  • Getting Started
  • Video
  • Blog

Android Spinner

Android Studio: Android beginner tutorial: Simple Spinner control using string array

May 7, 2018

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.

  1. Create the project and modify the main activity layout to add the spinner control.
    XHTML
    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
    <?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context=".MainActivity">
     
        <Spinner
            android:id="@+id/spinnerPlanet"
            android:layout_width="368dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
     
        <TextView
            android:id="@+id/txtPlanet"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="65dp"
            android:gravity="center_horizontal"
            android:text="TextView"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/spinnerPlanet" />
    </android.support.constraint.ConstraintLayout>

     
  2. Modify the string values xml file add the spinner string array values.
    XHTML
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <resources>
        <string name="app_name">HadSimpleSpinner</string>
     
        <string-array name="planets_array">
            <item>Mercury</item>
            <item>Venus</item>
            <item>Earth</item>
            <item>Mars</item>
            <item>Jupiter</item>
            <item>Saturn</item>
            <item>Uranus</item>
            <item>Neptune</item>
        </string-array>
        
    </resources>

     
  3. Modify the activity, create the spinner adapter and attach to the spinner control
    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
    package com.dhamen.hadsimplespinner;
     
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;
    import android.widget.TextView;
     
    public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
     
        TextView textViewPlanet;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            textViewPlanet = findViewById(R.id.txtPlanet);
     
            Spinner spinner = findViewById(R.id.spinnerPlanet);
            spinner.setOnItemSelectedListener(this);
     
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                    R.array.planets_array, android.R.layout.simple_spinner_item);
     
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     
            spinner.setAdapter(adapter);
     
     
        }
     
     
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            textViewPlanet.setText(parent.getItemAtPosition(position).toString());
        }
     
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
     
        }
    }

     
  4. Watch the Video tutorial

 

Filed Under: Android, Android Spinner

Primary Sidebar

Welcome

DhamenORM is very simple tool that will generate all the important database functions that are required by any relational database application, create, read, update, and delete (CRUD).

Recent Posts

  • Android Studio: Android beginner tutorial: Simple Spinner control using string array
  • Android Quick Tips: How to Pass Bundle to Second Activity Using Static Start Method
  • Android SQLite Data Access Objects classes generator
  • Create SQLite database programmatically using VB.NET
  • How to Populate ComboBox From SQLite Database Vb.Net

Categories

  • ADO.NET
  • Android
  • Android Spinner
  • SQLite
  • Uncategorized
  • VB.Net