Tuesday 14 April 2015

Android spinner

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.
package com.spinner;

import java.util.ArrayList;
import java.util.List;

import android.R.array;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> cnt=new ArrayList<String>();
cnt.add("India");
cnt.add("Pakistan");
cnt.add("Canada");
cnt.add("England");
Spinner spn=(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> adp=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,cnt);
spn.setAdapter(adp);
final List<String> in=new ArrayList<String>();
in.add("HP ");
in.add("Punjab");
in.add("Haryana");

final List<String> pak=new ArrayList<String>();
pak.add("Lahore");
pak.add("pak1");
pak.add("Pak2");


spn.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(position==0)
{
Spinner spn=(Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> adp1=new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item,in);
spn.setAdapter(adp1);
}
else if(position==1)
{
Spinner spn=(Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> adp1=new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item,pak);
spn.setAdapter(adp1);

}

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

Toast.makeText(getApplicationContext(), "login", 3000);
}

});

}
public class state {

int cntcode;
string statename;
}


}

No comments:

Post a Comment