Description
Spinner is like a drop-down menu that provides multiple options in the list and selects anyone. I have created a spinner which is used to bind with ArrayAdapter class. ArrayAdapter class is used to store string array and display into Spinner. It provides scrolling view through which the user can get the desired value. It is a subclass of AsbSpinner. It is associated with AdapterView so, we are using an Adapter class.
Components which is introduced in the article is Spinner, TextView, onSelectedItem method. This method is called when action is performed by selecting the value from the menu.
Attributes
android:dropDownHorizontalOffset: The amount of pixel by which the dropdown adjust itself Horizontally
android:dropDownVerticalOffset: The amount of pixel by which the dropdown adjust itself Vertically
android:dropDownWidth: To set the width of dropdown when SpinnerMode=”dropdown”
android: gravity: This property is used to set the position of the controls and content
android: prompt: The prompt appears when spinner DialogBox is display
android:popupBackground: When SpinnerMode=”drop-down” then the background drawable is used for drawable
android: SpinnnerMode: It provides display mode for spinner options
android: dropdownSelector: This attribute is used when SpinnerMode=”drop-down” is displayed
Methods
onClick(): This method is called when the button in d is clicked
performClick(): It is performed action when OnClickListener event is set
onTouchEvent(): This method is applied to handle touch event on screen
onSaveInstanceState(): It allows to generate view representation of the internal state which later can be used as a new instance
onResolvePointerIcon(): It returns pointer icon for motion event ,if it is specified otherwise returns nothing
getBaseline(): It returns the distance(offset) from widget text baseline from the top boundary of widget
OnItemSelected(): it returns the selected item from spinner dropdown list
Android_Mainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mapsample.msclient009.dropdown"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Main_Activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" 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="com.mapsample.msclient009.dropdown.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Select Item" android:textColor="#DDA1F1" android:textSize="30dp" android:gravity="center" android:background="#F1f1f1"/> <Spinner android:id="@+id/DropDown" android:layout_width="match_parent" android:layout_height="400dp" android:layout_weight="1" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:background="#99AA99" /> </LinearLayout>
Menuitem_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="china">Java</item>
<item android:title="indian">Android</item>
<item android:title="south">C++</item>
<item android:title="punjab">Hibernate</item>
<item android:title="p">Spring</item>
<item android:title="s">Struts2</item>
</menu>
Main_Activity.java
package com.mapsample.msclient009.dropdown; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; 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 Language[] = {"Java", "Android", "C++", "Python","Hibernate","Spring","Struts2"}; Spinner s; int count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_main ); s = (Spinner) findViewById ( R.id.DropDown ); s.setOnItemSelectedListener ( this ); ArrayAdapter arrayAdapter = new ArrayAdapter ( this, R.layout.support_simple_spinner_dropdown_item, Language ); arrayAdapter.setDropDownViewResource ( android.R.layout.simple_spinner_dropdown_item ); s.setAdapter (arrayAdapter); } public void onItemSelected(AdapterView<?> arg1, View sh,int p,long id) { if(arg1!=null) { count++; Toast.makeText ( getApplicationContext ( ), Language[p] + " List item :" + count, Toast.LENGTH_LONG ).show ( ); } } public void onNothingSelected(AdapterView<?> arg1) {Toast.makeText ( getApplicationContext (),"Nothing is selected",Toast.LENGTH_LONG ).show (); } public boolean onCreateOptionsMenu(Menu menu) {getMenuInflater ().inflate ( R.menu.menuitem_activity,menu ); return true; } }
Launch Your APP
Leave Comment