0% found this document useful (0 votes)
24 views8 pages

Input Controls Spinners

Mad

Uploaded by

atharv.saste232
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)
24 views8 pages

Input Controls Spinners

Mad

Uploaded by

atharv.saste232
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/ 8

Input Controls

Spinners
Spinner
• In Android, Spinner provides a quick way to select
one value from a set of values.
• Android spinners are nothing but the drop down-list
seen in other programming languages.
• In a default state, a spinner shows its currently
selected value.

• Spinner is
associated with
Adapter view so to
fill the data in
spinner we need to
use one of the
Adapter class.
Spinner - ArrayAdapter

• In android, An adapter is a bridge between UI component and


data source that helps us to fill data in UI component.
• It holds the data and send the data to adapter view then view
can takes the data from the adapter view and shows the data
on different views like listview, gridview, spinner etc.
• Whenever you have a list of single type of items which is backed
by an array, you can use ArrayAdapter. For instance, list of
phone contacts, countries or names.
• By default, ArrayAdapter expects a Layout with a single
TextView.
Spinner - ArrayAdapter
• Syntax:
• ArrayAdapter(Context context, int resource, int
textViewResourceId, T[] objects)
• context:
• The first parameter is used to pass the context means the
reference of current class. Here this is a keyword used to show
the current class reference. We can also use
getApplicationContext(), getActivity() in the place of this
keyword. getApplicationContext() is used in a Activity and
getActivity() is used in a Fragment.
• ArrayAdapter arrayAdapter = new ArrayAdapter(this, int
resource, int textViewResourceId, T[] objects);
Spinner - ArrayAdapter
• resource:
• The second parameter is resource id used to set the layout(xml file)
for list items in which you have a text view.
• ArrayAdapter arrayAdapter = new ArrayAdapter(this,
R.layout.list_view_items, int textViewResourceId, T[] objects);
• textViewResourceId:
• The third parameter is textViewResourceId which is used to set the id
of TextView where you want to display the actual text.
• ArrayAdapter arrayAdapter = new ArrayAdapter(this,
R.layout.list_view_items, R.id.textView, T[] objects);
• objects:
• The fourth parameter is an array of objects, used to set the array of
elements in the textView. We can set the object of array or array list
here.
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
• ArrayAdapter arrayAdapter = new ArrayAdapter(this,
R.layout.list_view_items, R.id.textView, bankNames);
Example of Spinner :Open res -> layout -> activity_main.xml
<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=".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>
Example of Spinner :open app-> java -> package -> MainActivity.java
package example.abhiandriod.spinnerexample; 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.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the bank name list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(), bankNames[position], Toast.LENGTH_LONG).show();}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}}
Menu in Android

Option Menu Context Menu Pop-up Menu

You might also like