How to disable an item in spinner? - android

I'm working on an android project. I am using spinner to show a drop down list. I want to make some items disable on the basis of some conditions. Following is code for what I have tried:
ArrayAdapter<CharSequence> statusArray = ArrayAdapter.createFromResource(getApplicationContext(), R.array.status_array, android.R.layout.simple_spinner_item);
statusArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Disable item at position 3
statusArray.setSelection(2, false);
// Set Adapter for Spinner
statusSpinner.setAdapter(statusArray);
This is not the exact code but similar to original one. I'm new to android. Any help would be appreciated.

Whenever an selection occur in spinner on condition call setEnabled method above setAdapter method
ArrayAdapter<CharSequence> statusArray = ArrayAdapter.createFromResource(getApplicationContext(), R.array.status_array, android.R.layout.simple_spinner_item);
statusArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Disable item at position 3
statusArray.setSelection(2, false);
//Set enable disable as follow
statusSpinner.setEnabled(true/false)
statusSpinner.setClickable(true/false)
// Set Adapter for Spinner
statusSpinner.setAdapter(statusArray);

Related

Set position of spinner from variable

I have an activity that recieves two variables called l_source and l_destination and I want to set the default values of two spinners equal to those variables. To do so, I have made a search and I understand that I need to get the position of the value of the variable (will give a number) and then set the new position of the spinner. The problem is that I am trying to use getAdapterPosition and I am sent Cannot resolve method getAdapterPosition(l_source).
EDIT
I finally managed to do it, but it is only working in one spinner. Have you got any idea of why? Here is my code:
Spinner spinner = (Spinner) findViewById(R.id.lista_origen);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getPosition(l_source));
Spinner spinner2 = (Spinner) findViewById(R.id.lista_destino);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(adapter2.getPosition(l_target));
Thank you for your time.
get position of selected item is spinner by int spinnerPosition = spinner.getSelectedItemPosition();

how to set position value of spinner according to clickable position of spinner by another spinner in android

i am trying to implement spinner position according to the position of other spinner ,these two spinners are in different fragments. these fragments are on tabhost. please help me hoe to do that.
this is my code
ArrayAdapter<String> arradp= new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, deviceidlist);
spinner.setAdapter(arradp);
String nme = ApplicationData.gettabalias();
if(!nme.equals(null)){
int position = arradp.getPosition(nme);
spinner.setSelection(position);
}

Setting the dropdown for the action bar button

I want to display a Drop down that contain the items from Array List when click on button on action bar.
Is there any good practice/tutorial to accomplish this?
i think you can somthink like that put the arraylist as the spinner database
Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
//adapter helps to add data to spinner.
ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_dropdown_item,user_spinner_string);
mySpinner.setAdapter(adapter);

Android programming spinner swap elements of spinner

need to do so when you click on a specific button (application type translator and arrow key) changed one of the selected items to another spinner itam swapped Well, here is how it can be implemented?
here is the code but it does not work!
int spinner1Index = spinner.getSelectedItemPosition();
spinner.setSelection(spinner2.getSelectedItemPosition());
spinner2.setSelection(spinnerfirst);
can offer something similar or fix!
You would have to change the adapter of your spinner and call notifyDataSetChanged()
I would suggest adding this to your switch case for when the item is selected.
Set a custom adapter for the spinner on item click.
//An array of what you want to populate the spinner with
Array spinnerArray [] = {"One", "Two", "Etc.."};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//Before setting the adapter, clear and notify the previous ArrayList used
arrayList.clear();
arrayList.notify();
spinner.setAdapter(spinnerArrayAdapter);

How to add/remove items in spinner adapter

In my application I have two spinner which one is using same adapter.
Spinner mSpinner1 = findSpinnerView(R.id.spinner1);
Spinner mSpinner2 = findSpinnerView(R.id.spinner2);
SpinnerCustomAdapter mAdapter = new SpinnerCustomAdapter(this,List<Food> foodList);
mSpinner1.setAdapter(mAdapter);
mSpinner2.setAdapter(mAdapter);
How could I remove or add items in adapter? More specifically when I select one of item that selected item should be remove when selecting another that items should be removed but the previous should be appear again.
I recommend that you do the deletion of spinner items this way:
foodList.remove(foodList.get(itempostoremove));
SpinnerCustomAdapter mAdapter = new SpinnerCustomAdapter(this, foodList);
mSpinner1.setAdapter(mAdapter);
you can change the foodList,add or delete
and use mAdapter.notifyDataSetChanged() to refresh view
OnItemSelected will be fired by this:
this.getAdapter().remove(currentWagon);
this.getAdapter().notifyDataSetChanged();
this.setAdapter(this.getAdapter());

Categories

Resources