Spinner in Android is nothing but drop down list and implementing this in android is very simple.
In below example there is one spinner where you can select continent name and selected item will be displayed in below TextView. ArrayList "continent" is being used to store all item of spinner, but if this list is bigger you may consider to use different java file.
We need to create:
- Activity
- MainActivity.java: contains java file and xml layout file
- MainActivity.java
- activity_main.xml
Source Codes:
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:layout_margin="15dp" tools:context=".MainActivity"> <TextView android:id="@+id/tvSelectContinent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="3dp" android:text="@string/select_continent" /> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_below="@+id/tvSelectContinent" android:background="@android:drawable/btn_dropdown" android:spinnerMode="dropdown" /> <TextView android:id="@+id/tvContinent" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25sp" android:layout_alignParentBottom="true" android:layout_marginBottom="40dp" android:gravity="center" /> </RelativeLayout>
MainActivity.java
package com.sushil.tech.simplespinner; import android.support.v7.app.ActionBarActivity; 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.TextView; public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener { TextView tvContinent; Spinner spinner; String[] continent = { "Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "South America"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvContinent = (TextView) findViewById(R.id.tvContinent); spinner = (Spinner) findViewById(R.id.spinner); spinner.setOnItemSelectedListener(this); // Create an ArrayAdapter using the string array and // a default spinner layout ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, continent); // 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); } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String con = continent[position]; tvContinent.setText(con); } @Override public void onNothingSelected(AdapterView<?> parent) { } }
0 comments:
Post a Comment