Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find whether if user clicked spinner in Android?

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

like image 239
Waugh Avatar asked Mar 15 '13 04:03

Waugh


3 Answers

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.

like image 20
SKK Avatar answered Sep 17 '22 20:09

SKK


you can use this:

 if (spin.getSelectedItemPosition() < 0) {//Do something}

this means user hasn't selected anything.

like image 141
Farhood Avatar answered Sep 19 '22 20:09

Farhood


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 

            }
        }
}
like image 31
Mr.Sandy Avatar answered Sep 18 '22 20:09

Mr.Sandy