Text in Spinner Becomes Faded After onCheckedChange From Radio Button - android

What would cause the text to become faded within the spinner upon choosing a radio button that changes the contents of the spinner? I haven't done anything to the xml file and was only changing contents. I have it coded that it doesn't do this on the loading of the activity, only on the click of a radio button.
I can edit and add any code requested, I'm just not sure what to post or what the problem would be.
Edit:
My Radio Group code
guntype = (RadioGroup) findViewById(R.id.category);
guntype.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int pos = guntype.indexOfChild(findViewById(checkedId));
if (pos == 0){
List<String> assaultrifles = new ArrayList<String>();
assaultrifles.add("R-201 Carbine");
assaultrifles.add("Hemlok BF-R");
assaultrifles.add("G2A5");
assaultrifles.add("V-47 Flatline");
ArrayAdapter<String> assaultRifleAdapt = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,assaultrifles);
assaultRifleAdapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(assaultRifleAdapt);
}
My spinner code:
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.assaultRifles, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()

Not sure if this would affect it but you use android.R.layout.simple_spinner_item and then you use android.R.layout.simple_spinner_dropdown_item. Try making them the same?

Related

Sorting the values in Spinner depending on the condition

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);
}

Swap selected items between spinners

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.

dynamically fill spinner based on selection in another spinner

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);

Android Single Spinner, Multiple Arrays

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.

How to connect 2 different spinners altogether

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);

Categories

Resources