Introduction
This blog helps to explain how to develop Spinner app in an Android Application, using an Android Studio.
Requirements
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.
Step 2
Here, you can create your Application name and choose where your project is stored on the location and click Next button.
Now, we can select the version of an Android; it is Target Android Devices.
Step 3
Here, we can add the activity and click Next button.
Now, we can write the activity name and click Finish button.
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.
Now, we can see the Graphical User Interface design.
Step 5
Here, you need to build on the design and write .XML code.
activity_mai.xml code.
- <?xml version="1.0" encoding="utf-8"?>
- <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">
- <Spinner android:id="@+id/simpleSpinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" />
- </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.
Now, we can see MainActivity.java code.
- package xyz.rvconstructions.www.spinnerapp;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- 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 implements AdapterView.OnItemSelectedListener {
- String[] UniversityNames = {
- "Anna",
- "oxford",
- "London Bridge",
- "Nehru",
- "Amit"
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
- spin.setOnItemSelectedListener(this);
- ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, UniversityNames);
- aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spin.setAdapter(aa);
- }
- @Override
- public void onItemSelected(AdapterView << ? > arg0, View arg1, int position, long id) {
- Toast.makeText(getApplicationContext(), UniversityNames[position], Toast.LENGTH_LONG).show();
- }
- @Override
- public void onNothingSelected(AdapterView << ? > arg0) {}
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- if (id == R.id.action_bar) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Step 7
Here, you will go to run it and select Run-> Run app option.
Here, you will choose Emulator or the devices; it is Nokia Nokia _X.
Step 8
Here, you can see the output.
Now,you will click the Anna and afterwards you will show that below output.
Now, you will click oxford and afterwards, you will see the output given below.