Spinner:
Spinner in android is used to show the required data in the form of Drop Down List.
OUT PUT Of The Tutorial:
OUT PUT Of The Tutorial:
Create Android Application:
- File >> New >> Android Application
- Enter Project name: SpinnerApp
- Package: com.ambilpursunil.newapp
- Keep other default selections, click Next until you reach Finish

1.Simply Copy and Past the Code which is display below in the MainActivity.java
package com.ambilpursunil.newapp;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import
android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
private String selectedCountry=null;
private String selectedAnimal=null;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner =(Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
createSpinnerDropDown();
}
private void createSpinnerDropDown() {
// TODO Auto-generated method stub
Spinner spinner = (Spinner)
findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("cat");
list.add("dog");
list.add("monkey");
list.add("donkey");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View
view,
int position, long id) {
// TODO Auto-generated method stub
String selectedItem =
parent.getItemAtPosition(position).toString();
switch(parent.getId()){
case R.id.spinner:
if(selectedCountry != null){
Toast.makeText(parent.getContext(),"Country
You Selected is" +selectedItem , Toast.LENGTH_LONG).show();
}
selectedCountry =
selectedItem;
break;
case R.id.spinner1:
if(selectedAnimal != null){
Toast.makeText(parent.getContext(),
"Animal Selected is" +selectedItem, Toast.LENGTH_LONG).show();
}
selectedAnimal =
selectedItem;
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
}
2.Simple Copy and Past the below code: actvitiy_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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" >
<TextView
android:id="@+id/tvSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/country_prompt"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/country_array"
android:prompt="@string/country_prompt"
/>
<TextView
android:id="@+id/tvSpinnerone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/animal_prompt"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/animal_prompt"
/>
</LinearLayout>
3.Copy and past the code for res=>value=>string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SpinnerDemo</string>
<string name="action_settings">Settings</string>
<string name="country_prompt">Choose a Country</string>
<string-array name="country_array">
<item>India</item>
<item>Denmark</item>
<item>Norway</item>
<item>Finlank</item>
<item>Sweden</item>
</string-array>
<string name="animal_prompt">Choose An Animal</string>
</resources>
4.Copy and past the code for manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambilpursunil.newapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ambilpursunil.newapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When our application launch you see two spinner one for Country which we define in string.xml and other is Animals we dedined in the program. If you click and choose the country spinner the it will display the list of options like india, denmark, norway etc if you click any country you will receive the Toast message.
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial... BlueTooth Functionality In Android:
No comments:
Post a Comment