android - how to use spinner - stack overflow

3
8/ 19/13 andr oid - how to use spinner - Stack Over fl ow st ack over f l ow.com/questi ons/2399086/how -to-use-spinner 1/ 3 Tell me more  × corotana 26 1 1 2 3 Answers I am very new to android. I want to use 2 spinners in my application, one shows the countries list, when any country is selected the other spinner should show the list of cities of that country. when city is selected some action is performed. plz help me with some sample code. thanks in anticipation android  spinner asked Mar 8 '10 at 3:16 1 Check this blog, it'll let you know something nice. I am also working on same concept, I'll put the answer here, when I get a complete solution. Regards, Haps. – Harpreet Feb 27 '12 at 11:12 Here is something what we can use to add options to spinner2 w.r.t to s pinner 1.  rea ;  }  }  @Override  public void onNothingSelected(AdapterView<?> arg0) {   // TO DO Au to-ge nerat ed m etho d stu b }  });  }  // o n-cre ate  @Override  public void onClick (View v)  {  switch (v.getId())  {  case R.id.btnSubmit: Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required. how to use spinner

Upload: ajay-j-verma

Post on 01-Jul-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

8/15/2019 Android - How to Use Spinner - Stack Overflow

http://slidepdf.com/reader/full/android-how-to-use-spinner-stack-overflow 1/3

8/19/13 android - how to use spinner - Stack Overflow

stackoverflow.com/questions/2399086/how-to-use-spinner

Tell me more   ×

corotana26 1 1 2

3 Answers

I am very new to android. I want to use 2 spinners in my application, one shows the countries list, when

any country is selected the other spinner should show the list of cities of that country. when city is

selected some action is performed. plz help me with some sample code. thanks in anticipation

android   spinner 

asked Mar 8 '10 at 3:16

1 Check this blog, it'll let you know something nice. I am also working on same concept, I'll put the answer 

here, when I get a complete solution. Regards, Haps. – Harpreet Feb 27 '12 at 11:12

Here is something what we can use to add options to spinner2 w.r.t to spinner 1.

 

rea ;

  }

  }

  @Override

  public void onNothingSelected(AdapterView<?> arg0) {

   // TODO Auto-generated method stub

}

  });

  } // on-create

  @Override

  public void onClick(View v)

  {

  switch (v.getId())

  {

  case R.id.btnSubmit:

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, noregistration required.

how to use spinner

8/15/2019 Android - How to Use Spinner - Stack Overflow

http://slidepdf.com/reader/full/android-how-to-use-spinner-stack-overflow 2/3

8/19/13 android - how to use spinner - Stack Overflow

stackoverflow.com/questions/2399086/how-to-use-spinner

Harpreet

712 5 24

  break;

  case R.id.btnCancel:

finish();

  break;

  default:

  break;

  } 

If you find this useful, then do give it up vote, so that others can find a good answer easily.

For each Country, you have to create a class for it, to just add State, City & Area. So that it doesn't

become a mesh at a single at single page.

Have fun.

Regards,

Haps.

answered Feb 27 '12 at 13:28

Here is a sample code which depicts the usage of spinner and action performed when spinner item is

selected

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {

  Spinner spin;

  String spin_val;

  String[] gender = { "Male", "Female" }; //array of strings used to populate t

@Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); //setting layout

spin = (Spinner) findViewById(R.id.spinner_id); //fetching view's id 

   //Register a callback to be invoked when an item in this AdapterView has

spin.setOnItemSelectedListener (new OnItemSelectedListener() {

  @Override

  public void onItemSelected(AdapterView<?> arg0, View arg1,

  int position, long id) { 

 // TODO Auto-generated method stub

spin_val = gender[position]; //saving the value selected 

  }

  @Override

  public void onNothingSelected(AdapterView<?> arg0) {

  TODO Auto- enerated method stub

8/15/2019 Android - How to Use Spinner - Stack Overflow

http://slidepdf.com/reader/full/android-how-to-use-spinner-stack-overflow 3/3

8/19/13 android - how to use spinner - Stack Overflow

stackoverflow.com/questions/2399086/how-to-use-spinner

Rachita Nanda

738 9

Karan

5,013 1 16 21

To add a list of values to the spinner, you then need to specify a SpinnerAdapter in your Activity, which

extends Adapter class.. A spinner adapter allows to define two different views: one that shows the data in

the spinner itself and one that shows the data in the drop down list when the spinner is pressed.You can

also download & refer to the complete spinner_demo example project for better understanding.

answered Oct 20 '12 at 10:41

Check the following examples :

http://developer.android.com/intl/fr/guide/tutorials/views/hello-spinner.html

http://www.designerandroid.com/?cat=4

answered Mar 8 '10 at 9:58

Not the answer you're looking for? Browse other questions tagged android   spinner

or ask your own question.