title: Check empty EditText depending on a Spinner
body: <p>I have a Spinner which goes from 0 to 10. When we select 5 for example, we get the 5th EditText. I want to check if the 5th EditText is empty after validation button.</p>
  
  <pre><code>    &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:maxLength="2"
          android:inputType="number"
          android:id="@+id/editAgeEnfants1"
          android:layout_below="@+id/ageEnfants"
          android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true"
          android:background="#EBEBEB"
          android:padding="5dp"
          android:layout_marginBottom="5dp"
          android:hint="Premier enfant"/&gt;
  
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:maxLength="2"
          android:inputType="number"
          android:id="@+id/editAgeEnfants2"
          android:layout_below="@+id/editAgeEnfants1"
          android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true"
          android:background="#EBEBEB"
          android:padding="5dp"
          android:layout_marginBottom="5dp"
          android:hint="Deuxième enfant"/&gt;
  
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:maxLength="2"
          android:inputType="number"
          android:id="@+id/editAgeEnfants3"
          android:layout_below="@+id/editAgeEnfants2"
          android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true"
          android:background="#EBEBEB"
          android:padding="5dp"
          android:layout_marginBottom="5dp"
          android:hint="Troisième enfant"/&gt;
  
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:maxLength="2"
          android:inputType="number"
          android:id="@+id/editAgeEnfants4"
          android:layout_below="@+id/editAgeEnfants3"
          android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true"
          android:background="#EBEBEB"
          android:padding="5dp"
          android:layout_marginBottom="5dp"
          android:hint="Quatrième enfant"/&gt;
  
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:maxLength="2"
          android:inputType="number"
          android:id="@+id/editAgeEnfants5"
          android:layout_below="@+id/editAgeEnfants4"
          android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true"
          android:background="#EBEBEB"
          android:padding="5dp"
          android:layout_marginBottom="5dp"
          android:hint="Cinquième enfant"/&gt;
  
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:maxLength="2"
          android:inputType="number"
          android:id="@+id/editAgeEnfants6"
          android:layout_below="@+id/editAgeEnfants5"
          android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true"
          android:background="#EBEBEB"
          android:padding="5dp"
          android:layout_marginBottom="5dp"
          android:hint="Sixième enfant"/&gt;
  
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:maxLength="2"
          android:inputType="number"
          android:id="@+id/editAgeEnfants7"
          android:layout_below="@+id/editAgeEnfants6"
          android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true"
          android:background="#EBEBEB"
          android:padding="5dp"
          android:layout_marginBottom="5dp"
          android:hint="Septième enfant"/&gt;
  
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:maxLength="2"
          android:inputType="number"
          android:id="@+id/editAgeEnfants8"
          android:layout_below="@+id/editAgeEnfants7"
          android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true"
          android:background="#EBEBEB"
          android:padding="5dp"
          android:layout_marginBottom="5dp"
          android:hint="Huitième enfant"/&gt;
  
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:maxLength="2"
          android:inputType="number"
          android:id="@+id/editAgeEnfants9"
          android:layout_below="@+id/editAgeEnfants8"
          android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true"
          android:background="#EBEBEB"
          android:padding="5dp"
          android:layout_marginBottom="5dp"
          android:hint="Neuvième enfant"/&gt;
  
      &lt;EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:maxLength="2"
          android:inputType="number"
          android:id="@+id/editAgeEnfants10"
          android:layout_below="@+id/editAgeEnfants9"
          android:layout_alignParentRight="true"
          android:layout_alignParentLeft="true"
          android:background="#EBEBEB"
          android:padding="5dp"
          android:layout_marginBottom="5dp"
          android:hint="Dixième enfant"/&gt;
  </code></pre>
  
  <p>I set the visible gone:</p>
  
  <pre><code>editAgeEnfants1.setVisibility(View.GONE);
  editAgeEnfants2.setVisibility(View.GONE);
  editAgeEnfants3.setVisibility(View.GONE);
  editAgeEnfants4.setVisibility(View.GONE);
  editAgeEnfants5.setVisibility(View.GONE);
  editAgeEnfants6.setVisibility(View.GONE);
  editAgeEnfants7.setVisibility(View.GONE);
  editAgeEnfants8.setVisibility(View.GONE);
  editAgeEnfants9.setVisibility(View.GONE);
  editAgeEnfants10.setVisibility(View.GONE);
  </code></pre>
  
  <p>The spinner: </p>
  
  <pre><code>    &lt;Spinner
          android:id="@+id/spinnerEnfantsCharge"
          style="@style/spinner_style"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_below="@+id/enfantsCharge"
          android:layout_marginBottom="10dp"
          android:layout_marginTop="10dp"
          android:entries="@array/enfantsCharge"
          android:paddingBottom="10dp"
          android:paddingTop="10dp" /&gt;
  </code></pre>
  
  <p>Strings.xml</p>
  
  <pre><code>&lt;string-array name="enfantsCharge"&gt;
      &lt;item&gt;0&lt;/item&gt;
      &lt;item&gt;1&lt;/item&gt;
      &lt;item&gt;2&lt;/item&gt;
      &lt;item&gt;3&lt;/item&gt;
      &lt;item&gt;4&lt;/item&gt;
      &lt;item&gt;5&lt;/item&gt;
      &lt;item&gt;6&lt;/item&gt;
      &lt;item&gt;7&lt;/item&gt;
      &lt;item&gt;8&lt;/item&gt;
      &lt;item&gt;9&lt;/item&gt;
      &lt;item&gt;10&lt;/item&gt;
  &lt;/string-array&gt;
  </code></pre>
  
  <p>How I display the EditText: </p>
  
  <pre><code>spinnerEnfantsCharge.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView&lt;?&gt; parentView, View selectedItemView, int position, long id) {
          if (position == 1) {
              ageEnfants.setVisibility(View.VISIBLE);
              editAgeEnfants1.setVisibility(View.VISIBLE);
              editAgeEnfants2.setVisibility(View.GONE);
              editAgeEnfants2.setText("");
              editAgeEnfants3.setVisibility(View.GONE);
              editAgeEnfants3.setText("");
              editAgeEnfants4.setVisibility(View.GONE);
              editAgeEnfants4.setText("");
              editAgeEnfants5.setVisibility(View.GONE);
              editAgeEnfants5.setText("");
              editAgeEnfants6.setVisibility(View.GONE);
              editAgeEnfants6.setText("");
              editAgeEnfants7.setVisibility(View.GONE);
              editAgeEnfants7.setText("");
              editAgeEnfants8.setVisibility(View.GONE);
              editAgeEnfants8.setText("");
              editAgeEnfants9.setVisibility(View.GONE);
              editAgeEnfants9.setText("");
              editAgeEnfants10.setVisibility(View.GONE);
              editAgeEnfants10.setText("");
          } ... else if (position == 10) {
              ageEnfants.setVisibility(View.VISIBLE);
              editAgeEnfants1.setVisibility(View.VISIBLE);
              editAgeEnfants2.setVisibility(View.VISIBLE);
              editAgeEnfants3.setVisibility(View.VISIBLE);
              editAgeEnfants4.setVisibility(View.VISIBLE);
              editAgeEnfants5.setVisibility(View.VISIBLE);
              editAgeEnfants6.setVisibility(View.VISIBLE);
              editAgeEnfants7.setVisibility(View.VISIBLE);
              editAgeEnfants8.setVisibility(View.VISIBLE);
              editAgeEnfants9.setVisibility(View.VISIBLE);
              editAgeEnfants10.setVisibility(View.VISIBLE);
          } else {
              ageEnfants.setVisibility(View.GONE);
              editAgeEnfants1.setVisibility(View.GONE);
              editAgeEnfants1.setText("");
              editAgeEnfants2.setVisibility(View.GONE);
              editAgeEnfants2.setText("");
              editAgeEnfants3.setVisibility(View.GONE);
              editAgeEnfants3.setText("");
              editAgeEnfants4.setVisibility(View.GONE);
              editAgeEnfants4.setText("");
              editAgeEnfants5.setVisibility(View.GONE);
              editAgeEnfants5.setText("");
              editAgeEnfants6.setVisibility(View.GONE);
              editAgeEnfants6.setText("");
              editAgeEnfants7.setVisibility(View.GONE);
              editAgeEnfants7.setText("");
              editAgeEnfants8.setVisibility(View.GONE);
              editAgeEnfants8.setText("");
              editAgeEnfants9.setVisibility(View.GONE);
              editAgeEnfants9.setText("");
              editAgeEnfants10.setVisibility(View.GONE);
              editAgeEnfants10.setText("");
          }
      }
  
      @Override
      public void onNothingSelected(AdapterView&lt;?&gt; parentView) {
          ageEnfants.setVisibility(View.GONE);
          editAgeEnfants1.setVisibility(View.GONE);
          editAgeEnfants1.setText("");
          editAgeEnfants2.setVisibility(View.GONE);
          editAgeEnfants2.setText("");
          editAgeEnfants3.setVisibility(View.GONE);
          editAgeEnfants3.setText("");
          editAgeEnfants4.setVisibility(View.GONE);
          editAgeEnfants4.setText("");
          editAgeEnfants5.setVisibility(View.GONE);
          editAgeEnfants5.setText("");
          editAgeEnfants6.setVisibility(View.GONE);
          editAgeEnfants6.setText("");
          editAgeEnfants7.setVisibility(View.GONE);
          editAgeEnfants7.setText("");
          editAgeEnfants8.setVisibility(View.GONE);
          editAgeEnfants8.setText("");
          editAgeEnfants9.setVisibility(View.GONE);
          editAgeEnfants9.setText("");
          editAgeEnfants10.setVisibility(View.GONE);
          editAgeEnfants10.setText("");
      }
  });
  </code></pre>
  
  <p>On my validation button I did:</p>
  
  <pre><code> else if ((spinnerEnfantsCharge.getSelectedItemPosition() == 1 &amp;&amp; editAgeEnfants1.getText().length() == 0) || (spinnerEnfantsCharge.getSelectedItemPosition() == 1 &amp;&amp; editAgeEnfants1.getText().length() == 0)) {
                              ageEnfants.requestFocus();
                              ageEnfants.setError("Veuillez entrer l'âge de vos enfants.");
  </code></pre>
  
  <p>But the code will be long if I check all conditions, I want to know if there is a simpler way to check the empty EditText? </p>
  
  <h2>SOLUTION</h2>
  
  <pre><code>if ((editAgeEnfants1.getVisibility() == View.VISIBLE &amp;&amp; editAgeEnfants1.getText().length() == 0) || (editAgeEnfants2.getVisibility() == View.VISIBLE &amp;&amp; editAgeEnfants2.getText().length() == 0) || (editAgeEnfants3.getVisibility() == View.VISIBLE &amp;&amp; editAgeEnfants3.getText().length() == 0) || (editAgeEnfants4.getVisibility() == View.VISIBLE &amp;&amp; editAgeEnfants4.getText().length() == 0) || (editAgeEnfants5.getVisibility() == View.VISIBLE &amp;&amp; editAgeEnfants5.getText().length() == 0) || (editAgeEnfants6.getVisibility() == View.VISIBLE &amp;&amp; editAgeEnfants6.getText().length() == 0) || (editAgeEnfants7.getVisibility() == View.VISIBLE &amp;&amp; editAgeEnfants7.getText().length() == 0) || (editAgeEnfants8.getVisibility() == View.VISIBLE &amp;&amp; editAgeEnfants8.getText().length() == 0) || (editAgeEnfants9.getVisibility() == View.VISIBLE &amp;&amp; editAgeEnfants9.getText().length() == 0) || (editAgeEnfants10.getVisibility() == View.VISIBLE &amp;&amp; editAgeEnfants10.getText().length() == 0)) {...}
  </code></pre>