0% found this document useful (0 votes)
75 views10 pages

Value Added (Spinner) Aim:-To Perform Programme of Spinner Pre-Requisite: - Android Studio Code

This document provides code to create a spinner in Android. It includes the XML layout code to define a spinner widget and add it to the UI. It also includes Java code for the main activity class to populate the spinner with sample data items, handle item selection, and display a toast notification of the selected item. The spinner allows the user to select a value from predefined categories for further processing.

Uploaded by

K. J kartik Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views10 pages

Value Added (Spinner) Aim:-To Perform Programme of Spinner Pre-Requisite: - Android Studio Code

This document provides code to create a spinner in Android. It includes the XML layout code to define a spinner widget and add it to the UI. It also includes Java code for the main activity class to populate the spinner with sample data items, handle item selection, and display a toast notification of the selected item. The spinner allows the user to select a value from predefined categories for further processing.

Uploaded by

K. J kartik Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

VALUE ADDED

(Spinner)

Aim:- To perform programme of Spinner

Pre-Requisite:- Android Studio

Code:-

/*Ac#vity_main.xml*\
<?xml version="1.0"
encoding="utf-8"?>

<androidx.constraintlayout.widge
t.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_pare
nt"


android:layout_height="match_par
ent"


tools:context=".MainActivity">


<TextView

android:id="@+id/
textView"


android:layout_width="wrap_conte
nt"


android:layout_height="wrap_cont
ent"

android:text="Category"


app:layout_constraintBottom_toBo
ttomOf="parent"


app:layout_constraintHorizontal_
bias="0.28"


app:layout_constraintLeft_toLeft
Of="parent"


app:layout_constraintRight_toRig
htOf="parent"


app:layout_constraintTop_toTopOf
="parent"


app:layout_constraintVertical_bi
as="0.153" />


<Spinner

android:id="@+id/
spinner"


android:layout_width="0dp"


android:layout_height="wrap_cont
ent"


android:layout_marginStart="1dp"


android:layout_marginLeft="1dp"


android:layout_marginTop="58dp"


android:layout_marginEnd="1dp"

android:layout_marginRight="1dp"


app:layout_constraintEnd_toEndOf
="parent"


app:layout_constraintStart_toSta
rtOf="parent"


app:layout_constraintTop_toBotto
mOf="@+id/textView" />


</
androidx.constraintlayout.widget
.ConstraintLayout>
/*MainAc#vity.java*\

package
com.example.spinner_example;


import
androidx.appcompat.app.AppCompat
Activity;


import android.os.Bundle;

import android.view.View;

import
android.widget.AdapterView;

import
android.widget.ArrayAdapter;

import android.widget.Spinner;


import java.util.ArrayList;

import java.util.List;

import
android.widget.AdapterView.OnIte
mSelectedListener;

import android.widget.Toast;



public class MainActivity
extends AppCompatActivity
implements
OnItemSelectedListener {


@Override

protected void
onCreate(Bundle
savedInstanceState) {

super.onCreate(savedInstanceStat
e);


setContentView(R.layout.activity
_main);


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


// Spinner click
listener


spinner.setOnItemSelectedListene
r(this);


// Spinner Drop down
elements

List<String> categories
= new ArrayList<String>();

categories.add("Select A
Value");


categories.add("Automobile");

categories.add("Business
Services");

categories.add("Computers");


categories.add("Education");


categories.add("Personal");


categories.add("Travel");


// Creating adapter for
spinner

ArrayAdapter<String>
dataAdapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_
item, categories);


// Drop down layout
style - list view with radio
button


dataAdapter.setDropDownViewResou
rce(android.R.layout.simple_spin
ner_dropdown_item);


// attaching data
adapter to spinner


spinner.setAdapter(dataAdapter);

}


@Override

public void
onItemSelected(AdapterView<?>
parent, View view, int position,
long id) {

String item =
parent.getItemAtPosition(positio
n).toString();


// Showing selected
spinner item


Toast.makeText(parent.getContext
(), "Selected: " + item,
Toast.LENGTH_LONG).show();

}


@Override

public void
onNothingSelected(AdapterView<?>
parent) {


}

}

Output :- 


You might also like