this may be a simple question but i was wondering, i have an application that has two spinners and a textview which displays a boolean. the two spinners have numbers between 1 and 10 and i want the boolean to display true when ever the numbers from the two spinners equal to 12. i have all the code to do this and to check are the numbers in the spinners = 12 but i dont know where to put the code to check are the numbers = 12 when ever one of the spinners are changed
so long story short, is there a onClick command i can use to call this code when ever someone clicks on a spinner and changes the number in the spinner?
Thanks
Get an instance of the spinner and add a listener for when an item is selected. That way it will trigger after the user has clicked the spinner, and selected a value.
Spinner s = (Spinner)findViewById(R.id.myspinner);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
You have to use an on item selected listener:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// Here you check the spinner values sum
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
onItemSelected()
http://developer.android.com/resources/tutorials/views/hello-spinner.html
Related
I want to develop an Android app that should run on devices with an external keyboard. The user should be able to go through a form using the enter key. Now, I have an issue with a Spinner and a Button.
The only way (I found) to transfer the focus from the Spinner to the Button is to use an setOnItemSelectedListener:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
root.findViewById(R.id.button).requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});
This works when the user selects another item from the Spinner than the current value. However, it doesn't work when the user opens the dialog (with entering), then doesn't select another value (but presses enter to confirm the current choice). I guess the OnItemSelected-Event isn't triggered when the value doesn't change.
Does anyone have a clue how I could implement that?
As the onItemSelected in the spinner is not called if you don't change the selection then you would have to do something like this
Force the user to select something from the spinner by adding a default value to the spinner like "Please select". Thus you could also show some error message if he doesn't select and eventually your onItemSelected listener would get invoked.
I ended up by using the solution from this answer: (Which extends the Spinner-class)
How can I get an event in Android Spinner when the current selected item is selected again?
Then, it is possible to shift the focus to the next item with
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
root.findViewById(R.id.Button).requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
activity_assessment.xml
assessment.java
assessment.java
arrays.xml
Eventhough , i am clicking on submit button i am unable to move to another activity.
Can you please help me in this how to navigate to other new activity by using two spinners and a submit button?
Above i attached my source code files
DON'T USE == in the if statement if(SpinnerValue1)
USE
.equals("TEST-1")
Like:
if(SpinnerValue1.equals("TEST-1"))
because SpinnerValue is an Object and you can't logically compare objects with "=="
See this
Also, I have this simple way of working with spinners :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Instead of using getSelected(), use position
spinnerValue = position;
//or
useValueFrom(yourDataArrayAt[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I have an issue which I just cannot get my head around. In my experience, code follows the sequence it is run in, but this does not appear to be the case in this instance.
I have two UI items, namely a spinner and an edittext field which are populated from a DB table in a function called from onCreate(). When the spinner is set (using setSelection) it kicks off the onItemSelected which sets the edittext value to a default value. After this, I then set the edittext field (using setText) to the value stored in the DB, but the new value does not appear.
Here is a snippet of the code:
spinnerdresslength.setSelection(backuplengthoptionid);
edittextskirtshortestlength.setText(backupshortestlength);
The event handler looks like as follows (I have simplified it for the sake of ease):
spinnerdresslength.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
edittextskirtshortestlength.setText("43");
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});`
Now despite the edittextskirtshortestlength.setText(backupshortestlength) following the spinner setSelection, the edittext shows 43 instead of backupshortestlength.
Can you anybody assist with this as I am at my wits end?
Thanks,
NicVegas
I have problem with android. I'm adding data from adapter to spinner, then I set the selection on item on position 0, after that I create OnItemSelectedListener and it is triggered but I don't know why...
onCreate method:
this.spinner = (Spinner)findViewById(R.id.spinner);
this.spinner.setAdapter(MyAdapter.GetAdapter());
this.spinner.setSelection(0);
this.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do some things
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Maybe here is something wrong, I also try to remove the listener, change the selection and then add listener to it. Maybe I do sth wrong here:
AdapterView.OnItemSelectedListener listener = this.spinner.getOnItemSelectedListener();
this.spinner.setOnItemSelectedListener(null);
this.spinner.setSelection(((SpinnerAdapter) this.spinner.getAdapter()).getElementIndexByTitle(array.get("ProductName")));
listener.onItemSelected(null, null, -1, -1);
this.spinner.setOnItemSelectedListener(listener);
It seems that it triggers the event also after: this.spinner.setOnItemSelectedListener(listener); Maybe is there possibility to select item without triggering OnItemSelected ?
I found out that inside the AdapterView there is a SelectionNotifier and it wokrs automatically. So I need to remember last selection index and then compare it with position in OnItemSelected.
The java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead," but that is not correct. I am using setOnItemClickListener to do some stuff based on the new selection, but I also need to do some stuff before the user changes the selection. Specifically, I am collecting data for each selection that needs to be saved to a file before moving to another selection, since the other selection is associated with different set of data. Is there a way to use setOnClickListener with an Android Spinner?
spinner.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do some stuff before the user changes the selection
...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// Do some stuff based onItemSelected
...
You can replicate the an onclick event using ontouch events
this.spinner=(Spinner)findViewById(R.id.spinner);
this.spinner.setClickable(false);
this.spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "spinner touch");
//replicating a click
if(event.getAction() == MotionEvent.ACTION_UP){
v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
}
return true;
}
});
You will have to set the Click listener on the underlying view (normally a TextView with id: android.R.id.text1) of the spinner. To do so:
Create a custom Spinner
In the constructor (with attributes) create the spinner by supplying the layout android.R.layout.simple_spinner_item
Do a findViewById(android.R.id.text1) to get the TextView
Now set the onClickListener to the TextView