In my application I am creating spinner dynamically in code.I want to force user to click on Spinner and change its value/content. Otherwise user should not be able to go to next screen by clicking Next button.
How to do that in Android? Anybody has any idea?
Thanks in Advance.
Rohan
Let the Spinner's first value be something like "-please select-".
when the user clicks on the next button perform validation and check whether the value of the selectedItem in the spinner is "-please select-" and if yes, then display a toast and ask the the user to select something from the spinner.
you need code, let me know.
you can use this:
if (spin.getSelectedItemPosition() < 0) {//Do something}
this means user hasn't selected anything.
Use this code for checking the spinner item is selected or not.
both flags take at class level (Globally).
Boolean temp = false;
Boolean check = false;
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(temp){
check = true;
}
temp = true;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
check = false;
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(check){
//perform when user select spinner item
}else{
//put Dialog for alert please select spinner item
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With