Disable buttons based on spinner choice - android

So I have buttons (not the soft key pad) on the screen but I want some to be disabled (can't click) when you pick a certain spinner option. Like I have the buttons 0-9 (for numeric input) and if "Base 2" (spinner selection 0) is picked I want all the buttons except 0 and 1 to be disabled.

You can add a OnItemClickListener and react to the option that is given like for example this way
spinner.setOnItemClickListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
if(position == 1)
button.setClickable(false);
}
});

Spinner does not support setOnItemClickListener. If you try and use it, you will get an exception:
java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner
As such, you should use setOnItemSelectedListener:
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
Toast.makeText(MyActivity.this, "position= "+position+" / id= "+id, Toast.LENGTH_LONG).show();
switch(position) {
case 0:
button0.setClickable(true);
button1.setClickable(false);
break;
case 1:
button0.setClickable(false);
button1.setClickable(true);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});

Related

Spinner get position and do different action for each selection

I am trying to use a spinner, save the position of each selection and depend on this, another spinner with different selections for each position appears..
I made the first spinner with the help of the spinner docs but then I can't do something different for each selection.. Is there anyone who can help me?
Thank you!
You can use a switch statement in the onItemSelected() method:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
switch (pos) {
case 0:
//do action for first selection
break;
case 1:
//do action for second selection
break;
...
}
}
it is too simple to impliment:
try spinner ItemSelectedListener.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(position==0){}//do the function you want to perform
else if(position==1){}//And so on
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

set spinner item selected value in onitemclick android

Hello friends i have spinner as following
When i click on any item click event than it should be set like following
it means when i select Afghnistan at that time it should be set value as AF and rest of country value remain as it is my code is as follow
On Spinner item click
mSpinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
mStringCountryID=mArrayListCountryDatas.get(position).getId();
mSpinnerCountry.setPrompt(mArrayListCountryDatas.get(position).getIso_alpha()+"
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
When i run above code it is not set value any idea how can i make it possible ?
mSpinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int mStringCountryID = s1.getSelectedItemPosition(); Toast.makeText(getBaseContext(),
"You have selected item : " + presidents[mStringCountryID],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this... variable mStringCountryID will hold your specific complete value do what ever you want to do with this value.

Display another arraylist element in textview using onItemSelected of a spinner

I have 2 arraylists, 1 is use to display the elements to the spinner and another one is use to display on a textview when one of the element from spinner is selected.
Example:
0---a---football
1---b---badminton
2---c---basketball
"a,b,c" are the elements in arraylist1; "football, badminton, basketball" are the elements in arraylist2; "0,1,2" are the index for both arraylists.The index of elements on both of the arraylists has already arranged properly as shown above.
What I want to do now is to let the spinner to display "a,b,c". When I select "b" in the spinner, the textview will show me "badminton".
What should I write in the onItemSelected of the spinner?Any idea for this?
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
switch(position){
case 0:
textView1.setText(sportsList.get(0));
break;
case 1:
textView2.setText(sportsList.get(1));
break;
case 2:
textView3.setText(sportsList.get(2));
break;
}
}
});
You can get the selected item in the spinner by this code
String selected = spinner1.getText().toString();
Then check for the condition,
if(selected.equals("a")){
textview1.setText(array2.get(0));
}else if(selected.equals("b")){
......
try this,
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Textview your_tv = (Textview)findviewbyid(R.id.tv);
your_tv.setText(your_array[position]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});

get which spinner is clicked

I took multiple spinners in single view. I know how to get when item is selected.
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id)
{
parent.getItemAtPosition(pos);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
But how can i know that which spinner user has clicked on in a single listener?
Any suggestion will be appreciated.
Thanks In advance.
Try the following way.
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
switch(parent.getId()){
case R.id.spinner1:
//your code goes here
break;
case R.id.spinner2:
//your code goes here
break;
}
}
Refer here and here too.
parent in onItemClick refers to the Spinner that was clicked.
AdapterView.OnItemSelected
parent The AdapterView where the selection happened
Spinner is an AdapterView. Spinner

How can I use an Android Spinner object to load other activities?

i want a spinner when select an item from the spinner its should load the corresponding java page that has been set.can we load a java page on selecting an item from spinner in android if so how can we implement this can any one provide some sample code
You can
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
switch(position) {
//Use cases to set Intents
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// Do Nothing
}
});
String[] items ={“One”,“Two”,“Three”,“Four”,“Five”};
Spinner sp = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item,items);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
switch(position){
case 0:
//call first class
break;
case 1:
//call second class
break;
case 2:
//call third class
break;
case 3:
//call fourth class
break;
default:
break;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
//nothing selected from spinner
}
});
just use spinner.onItemSelectedListener(new OnItemSelectedListener())
and in onItemSelected(AdapterView adapterview, View view, int position, long id) method body write your logic to start new activity based on the position.

Categories

Resources