res/layout/activity_main.xml
Step 1: spinner_sample.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:background="@drawable/background"
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/spinner1"
android:layout_width="160px"
android:layout_height="wrap_content"
android:background="@drawable/spin"
/>
</RelativeLayout>
Step 1: spinner_sample.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:background="@drawable/background"
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/spinner1"
android:layout_width="160px"
android:layout_height="wrap_content"
android:background="@drawable/spin"
/>
</RelativeLayout>
Step 2: MainActivity.java
package com.app.sample;
import android.app.Activity;
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 Activity {
Spinner spin1 ;
String[] str= {"Sachin Tendulkar" ,"Sehwag","Sourav Ganguly" ,"Gouham Gambir" ,"Yuvi" ,"Dhoni","Rahul Dravid","Saina Nehwal","Anand","KapilDev"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin1=(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> aa=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item,str);
spin1.setAdapter(aa);
spin1.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int position = spin1.getSelectedItemPosition();
Toast.makeText(getApplicationContext(),"You have selected "+str[+position],Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
);
}
}
Screen Shots :
Source Code : Click here to Download Source Code
Happy Programming.....
Cheers....!!!!!
Source Code : Click here to Download Source Code
Happy Programming.....
Cheers....!!!!!
No comments:
Post a Comment