Create A Spinner App In Android Application Using Android Studio

Introduction

This blog helps to explain how to develop Spinner app in an Android Application, using an Android Studio.

Requirements

  • Android Studio 2.1.3

If you want to create Spinner app, it should follow the steps given below. 

Step 1

Now, open Android Studio and you can choose the file, followed by selecting New. Afterwards, choose New Project.

Android

Step 2

Here, you can create your Application name and choose where your project is stored on the location and click Next button.

Android

Now, we can select the version of an Android; it is Target Android Devices.

Android

Step 3

Here, we can add the activity and click Next button.

Android

Now, we can write the activity name and click Finish button.

Android

Step 4

Now, open your project and you will go to the activity_main.xml. Afterwards, you will build the design. You should choose the toolbox and if you want some options (Spinner), use the drag and drop method.

Android

Now, we can see the Graphical User Interface design.

Android

Step 5

Here, you need to build on the design and write .XML code.

activity_mai.xml code.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="xyz.rvconstructions.www.spinnerapp.MainActivity">  
  3.     <Spinner android:id="@+id/simpleSpinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" />  
  4. </RelativeLayout>  
Step 6

Now, you will go to the MainActivity.java page and build Java code.

First of all, you will declare a file, which is an extension file.

Android

Now, we can see MainActivity.java code.
  1. package xyz.rvconstructions.www.spinnerapp;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.view.Menu;  
  5. import android.view.MenuItem;  
  6. import android.view.View;  
  7. import android.widget.AdapterView;  
  8. import android.widget.ArrayAdapter;  
  9. import android.widget.Spinner;  
  10. import android.widget.Toast;  
  11. public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {  
  12.     String[] UniversityNames = {  
  13.         "Anna",  
  14.         "oxford",  
  15.         "London Bridge",  
  16.         "Nehru",  
  17.         "Amit"  
  18.     };  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);  
  24.         spin.setOnItemSelectedListener(this);  
  25.         ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, UniversityNames);  
  26.         aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  27.         spin.setAdapter(aa);  
  28.     }  
  29.     @Override  
  30.     public void onItemSelected(AdapterView << ? > arg0, View arg1, int position, long id) {  
  31.         Toast.makeText(getApplicationContext(), UniversityNames[position], Toast.LENGTH_LONG).show();  
  32.     }  
  33.     @Override  
  34.     public void onNothingSelected(AdapterView << ? > arg0) {}  
  35.     @Override  
  36.     public boolean onOptionsItemSelected(MenuItem item) {  
  37.         int id = item.getItemId();  
  38.         if (id == R.id.action_bar) {  
  39.             return true;  
  40.         }  
  41.         return super.onOptionsItemSelected(item);  
  42.     }  
  43. }  
Step 7

Here, you will go to run it and select Run-> Run app option.

Android

Here, you will choose Emulator or the devices; it is Nokia Nokia _X.

Android

Step 8

Here, you can see the output.

Android

Android

Now,you will click the Anna and afterwards you will show that below output.

Android

Now, you will click oxford and afterwards, you will see the output given below.

Android