Spinners provide a quick way to select one value from a set.
In the default state, a spinner shows its currently selected value. Touching
the spinner displays a dropdown menu with all other available values, from
which the user can select a new one.
or
The spinner is nothing
but a label with a dropdown list often seen on web page forms. When the user
clicks this label, the dropdown list appears, and this allows the users to make
a selection based on this list. The “ItemClick” event is triggered when the
user makes a selection.
It’s effective in input
control and makes the developer’s work easier and reduces overhead of complex
designs. The most important feature of the spinner is that it prevents
excessive space utilization, which otherwise would be required to list items.
Generally, a Spinner
Adapter is required to pass the values to the spinner. A Spinner Adapter acts
as a bridge between the spinner and the data to be listed. It lists the values
in the form of a drop down list.
Program
activity_main.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#12E7FF"
>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#12E7FF"
>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1=(TextView)findViewById(R.id.tv1); Spinner spinner = (Spinner) findViewById(R.id.spinner); // Create an ArrayAdapter using the string array and a default spinner layout ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item); // Specify the layout to use when the list of choices appears adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Apply the adapter to the spinner spinner.setAdapter(adapter); } }
and place this code in res/value.string.xml
<resources> <string name="app_name">TestOne</string> <string name="hello">Hello</string> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> </string-array> </resources>
spinner mode
It defines display mode for spinner options. This Property
has two options: Dialog mode and DropDown mode. Dialog mode uses a dialog
window for selecting spinner options and drop-down mode uses drop-down anchored
to the Spinner for selecting spinner options.
Syntax -
android:spinnerMode="dialog"
Dropdown Width
Defines the width of
the dropdown list of spinner using setDropDownWidth() method.
Syntax - android:dropDownWidth="200dp"
Popup Background
Background drawable to use for the dropdown
It may be image or color.
syntax -
android:popupBackground="@drawable/image1"
OnItemselectedListner
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#12E7FF" > <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layerType="hardware" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv1" android:textSize="30dp" android:hint="Hint" /> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity { TextView textView1; Spinner spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1=(TextView)findViewById(R.id.tv1); spinner = (Spinner) findViewById(R.id.spinner); List<String> list1=new ArrayList<String>(); list1.add("Android"); list1.add("IOS"); list1.add("Window"); list1.add("Blackberry"); ArrayAdapter<String> arrayAdapter1=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,list1); arrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(arrayAdapter1); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String str=spinner.getSelectedItem().toString(); textView1.setText(str); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } }
22
No comments:
Post a Comment