I want to make two spinners, the first spinner have the list of states and second spinner contains the list of cities . If i select the particular state from the first spinner then next spinner must show only the cities on the selected state only .
My android code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner state = (Spinner) findViewById(R.id.spinnerState);
ArrayAdapter<CharSequence> stateadapter = ArrayAdapter.createFromResource(
this, R.array.item_state, android.R.layout.simple_spinner_item);
stateadapter.setDropDownViewResource(R.layout.spinner_layout);
state.setAdapter(stateadapter);
Spinner city = (Spinner) findViewById(R.id.spinnerCity);
ArrayAdapter<CharSequence> cityadapter = ArrayAdapter.createFromResource(
this, R.array.item_city, android.R.layout.simple_spinner_item);
cityadapter.setDropDownViewResource(R.layout.spinner_layout);
city.setAdapter(cityadapter);
}}
I have created all my for the state and cities.
There are lots of ways you can achieve this, for example:
ArrayAdapter<CharSequence> stateadapter;
switch(state)
{
case "Florida":
{
stateadapter = ArrayAdapter.createFromResource(this, R.array.cities_florida, android.R.layout.simple_spinner_item);
} break;
}
(kinda hardcoded)
The most optimal solution is to define it on an xml file (maybe you can get this somewhere on the internet) and write a class that reads the file and return all the cities on the selected state.
read:
https://developer.android.com/training/basics/network-ops/xml.html
Add a listener to the Spinner state
state.setOnItemSelectedListener(this);
Implement the listener:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedState = state.getSelectedItem().toString();
List<String> citiesInState = new ArrayList<>();
// add all cities in selectedState to this list using citiesInState.add();
// this will depend upon how you are storing the cities and states
ArrayAdapter<String> cityDataAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, citiesInState);
cityDataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(cityDataAdapter);
}
Related
I have a problem changing the items from spinner1 to spinner2. Ok im creating a translator. I have a spinner1 with some languages and a second spinner with the same languages. I select a "from" language from spinner1 and i select a"to" language from spinner2. Ok, all I want to do is that when I press the button it swaps the selected items of spinners. I don't know how implement this.
Here my code:
public class Example extends Activity {
ImageView MyChangeButton;
Spinner spinner1, spinner2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyChangeButton = (ImageView)findViewById(R.id.imageView1);
MyChangeButton.setOnClickListener(MyChangeButtonOnClickListener);
ArrayAdapter<CharSequence> adapter
= ArrayAdapter.createFromResource(this,
R.array.firstlanguage, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
}
private ImageView.OnClickListener MyChangeButtonOnClickListener = new ImageView.OnClickListener(){
public void onClick(View v) {
------>//Here the code that i want to do
}
};
};
what I understood from your question is, you want to swap values between two spinners.
If both of these spinners are using same source you can do something like below.
int spinner1Index = spinner1.getSelectedItemPosition();
spinner1.setSelection(spinner2.getSelectedItemPosition());
spinner2.setSelection(spinner1Index );
Let me know if this work for you, or else we will find another solution.
Im filling two spinners with text arrays from an xml resource file. depending on the selection in one spinner i want to fill the other with one of two different arrays from the xml resource file. im having trouble doing this in mono for android and cant find any examples online. please help
try this...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if(pos==0){
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arrayone, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
else{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arraytwo, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
your suggestion was a big help. With some changes, here is what worked for me in mono for android:
I have a spinner name spin3. You can select opt1 or opt2. There is a string array in the resources named opt1 and another named opt2. Then a second spinner (spinner4) changes its content to opt1 or opt2 based on what is selected in spin3.
public void spin3_onItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spin = (Spinner)sender;
string pos = spin.SelectedItem.ToString();
if(pos=="opt1")
{
Spinner spinner = (Spinner)FindViewById(Resource.Id.spinner4);
ArrayAdapter adapter = ArrayAdapter.CreateFromResource this, Resource.Array.opt1, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;
}
else
{
Spinner spinner = (Spinner) FindViewById(Resource.Id.spinner4);
ArrayAdapter adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.opt2, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;
}
}
Then to call the event use this
spin3.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spin3_onItemSelected);
I haven't been able to find the answer to this anywhere (it's quite possible I'm not searching right). In Android I'm trying to make a single spinner in my app pull from multiple arrays depending on which radio button is selected. If radio button 1 is selected I want the spinner to use array 1, if radio button 2 is selected I want the spinner to use array 2. Here is what I have so far, but it doesn't work. Everytime I click on the tab in my app with this code my app force closes.
public class This_Activity extends Activity {
/** Called when the activity is first created. */
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_2_layout);
rb1=(RadioButton)findViewById(R.id.Radio1);
rb2=(RadioButton)findViewById(R.id.Radio2);
rb3=(RadioButton)findViewById(R.id.Radio3);}{
if(rb1.isChecked() == true){
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
}
if(rb2.isChecked() == true){
Spinner spinner2 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array2, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
}
if(rb3.isChecked() == true){
Spinner spinner3 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array3, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(adapter);
}
}
}
It is difficult to diagnose the problem without the error messages you are seeing. Could you edit your question and include those?
However, one thing I am noticing is that you are creating different spinners in each of those cases. Try using
Spinner spinner = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = null;
above your if statements. Then the body of your if statements will look like
adapter = ArrayAdapter.createFromResource(
this, R.array.Array1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
you also probably want to hook up listeners to your radio buttons to change the spinner automatically when the user clicks on them, because as it is now this will only happen in the constructor.
rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// change your spinner adapter here
}
});
it is probably easier to put your radio buttons in a RadioGroup and put the OnCheckChangedListener on that, so you only need one listener for all 3 buttons.
I have to develop the application based on a spinner.
I have taken the spinner1 as the product choice and I have to give the spinner2 the data, which is in that particular product category so I have to update spinner2 on the base choice of spinner1.
I have search a lot but I can get any sure example code or resource for it.
I agree with #user639183, there are lot's of similar questions... however, there some explanation on how you do it:
Create member variables for the arrays containing your options to display.
Populate your first spinner with your category values.
Hook on to the Spinner's OnItemSelected event by using spinner1.setOnItemSelectedListener(...)
In the event listener, populate your second Spinner with the corresponding values.
Example for step 1:
private String[] spinner1values = new String[] { "cat1", "cat2" };
private String[][] spinner2values = new String[][] {
new String[] { "a1", "b1", "c1" },
new String[] { "a2", "b2" }
};
Population of spinner1 as follows:
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinner1values);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
This has so far been absolutely straight forward, if you read the documentation and examples for Spinners!
Next, hook on to the OnItemSelectedListener:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// create a new adapter with the corresponding values
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
android.R.layout.simple_spinner_item, spinner2values[position]);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set adapter
((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// nothing selected, so set empty options
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
android.R.layout.simple_spinner_item, new String[0]);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
}
});
Pay attention, that the order of the arrays in spinner2values is corresponding to the order of the category values!
I am trying to connect 2 spinners together. Meaning, the items inside 2nd spinner will depend on whatever item is chosen for the 1st spinner.
This is the code inside the main java file.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
This is the code inside MyOnItemSelectedListener.java
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext()), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
So the onItemSelected function will print out the item that was chosen in 1st spinner on the screen. However, I can't figure out how to create the 2nd spinner fully based on the value inside 1st spinner.
I know that there should be something needed to be done inside onItemSelected, but I just can't figure it out since I am a newbie in Java Android.
Can you guys assist me on this?
Thank you.
You should just do something very similar to what you do in onCreate, only with the other spinner. For example:
List<String> values = findValuesBySelection(parent.getItemAtPosition(pos).toString());
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, values, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);