0

I have a Button:

<Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="text"
                android:id="@+id/test"
                android:onClick="test"
                android:visibility="gone"/>

and I have 3 Spinners and I would that when ALL 3 spinners have a value selected, button visibility change in Visible.

Are there a differents ways for do this without check any time that I select spinner if 3 values is selected? Are there "watcher" for this type of operation?

EDIT

This is my function that in this moment I call at onItemSelected in spinners:

public void checkSpinner(){
        Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
        assert spinner1 != null;
        int position1= spinner1.getSelectedItemPosition();

        Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
        assert spinner2 != null;
        int position2=spinner2.getSelectedItemPosition();

        Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
        assert spinner3 != null;

        int position3=spinner3.getSelectedItemPosition();
        Button button = (Button) findViewById(R.id.button);
        if((position1>0) && (position2>0)&&(position3>0))
        {

            button .setVisibility(View.VISIBLE);
        }else{
            button .setVisibility(View.GONE);
        }
    }
LorenzoBerti
  • 6,704
  • 8
  • 47
  • 89
  • I have make a function that launch every time select one spinner, and I ask if are there other ways, or a way like "one watch" that check if values are selected – LorenzoBerti Aug 04 '16 at 13:56
  • Can you update your question with the code? – Sufian Aug 04 '16 at 14:37
  • The method seems to be okay except few performance issues. When do you call this `checkSpinner()`? – Sufian Aug 05 '16 at 07:16
  • I call this method in `onItemSelected(AdapterView> parent, View view, int position, long id) { checkSpinner(); }` In all 3 spinner – LorenzoBerti Aug 05 '16 at 07:29
  • I guess you're facing the same issue as this. Check if any of the solutions there work - http://stackoverflow.com/questions/4923310/android-spinner-onitemselected-setonitemselectedlistener-not-triggering – Sufian Aug 05 '16 at 07:37
  • I'm Looking, but this thread talk about not trigger event, but my event is triggered, maybe I explained bad, sorry for my English. I ask if there is a method for not having throw any time onItemSelected in 3 different spinner? A sort of watcher for all spinners in activity? I mean like $watch on angularjs for web development. – LorenzoBerti Aug 05 '16 at 07:47
  • If I understand it clearly, you need some sort of callback which only fires when selection has been made in all 3 Spinners. If this is the case, it might be possible through RxJava. You can watch [this video](https://www.youtube.com/watch?v=k3D0cWyNno4) as a tutorial. If you're familiar with RxJava then just check out the `combineLatest` thing in [this tutorial](https://github.com/kaushikgopal/RxJava-Android-Samples), I recall that the video talked about it. If you come up with a way what you want, post it as answer (and mark it) + update your question as well. It will be helpful for others. – Sufian Aug 05 '16 at 10:06

1 Answers1

0

Get the values of the selecteditem from all the 3 spinners and then compare the values with your desired values,if they are equal then show the button.

String text1=spinner1.getItemAtPosition(spinner.getSelectedItemPosition()).toString();

String text2=spinner2.getItemAtPosition(spinner.getSelectedItemPosition()).toString();

String text3=spinner3.getItemAtPosition(spinner.getSelectedItemPosition()).toString();

if(text1.equals("your desired value") && text2.equals("your desired value") && text3.equals("your desired value"))
{
  button.setVisibility(View.VISIBLE);
}
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
kgandroid
  • 5,507
  • 5
  • 39
  • 69