2

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.

    <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"/>

    <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"/>

    <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"/>

    <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"/>

    <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"/>

    <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"/>

    <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"/>

    <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"/>

    <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"/>

    <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"/>

I set the visible gone:

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);

The spinner:

    <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" />

Strings.xml

<string-array name="enfantsCharge">
    <item>0</item>
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
</string-array>

How I display the EditText:

spinnerEnfantsCharge.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> 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<?> 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("");
    }
});

On my validation button I did:

 else if ((spinnerEnfantsCharge.getSelectedItemPosition() == 1 && editAgeEnfants1.getText().length() == 0) || (spinnerEnfantsCharge.getSelectedItemPosition() == 1 && editAgeEnfants1.getText().length() == 0)) {
                            ageEnfants.requestFocus();
                            ageEnfants.setError("Veuillez entrer l'âge de vos enfants.");

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?

SOLUTION

if ((editAgeEnfants1.getVisibility() == View.VISIBLE && editAgeEnfants1.getText().length() == 0) || (editAgeEnfants2.getVisibility() == View.VISIBLE && editAgeEnfants2.getText().length() == 0) || (editAgeEnfants3.getVisibility() == View.VISIBLE && editAgeEnfants3.getText().length() == 0) || (editAgeEnfants4.getVisibility() == View.VISIBLE && editAgeEnfants4.getText().length() == 0) || (editAgeEnfants5.getVisibility() == View.VISIBLE && editAgeEnfants5.getText().length() == 0) || (editAgeEnfants6.getVisibility() == View.VISIBLE && editAgeEnfants6.getText().length() == 0) || (editAgeEnfants7.getVisibility() == View.VISIBLE && editAgeEnfants7.getText().length() == 0) || (editAgeEnfants8.getVisibility() == View.VISIBLE && editAgeEnfants8.getText().length() == 0) || (editAgeEnfants9.getVisibility() == View.VISIBLE && editAgeEnfants9.getText().length() == 0) || (editAgeEnfants10.getVisibility() == View.VISIBLE && editAgeEnfants10.getText().length() == 0)) {...}
  • possible duplicate of [compare string with equals method and ==](http://stackoverflow.com/questions/12524005/compare-string-with-equals-method-and) – Nabin Jul 06 '15 at 16:24

3 Answers3

0

You could check it with

editTextName.getText().toString().equals("")
Boe-Dev
  • 1,585
  • 2
  • 14
  • 26
0

You could check this with :

TextUtils.isEmpty(mEditText.getText().toString())

Note: Why you don't use a list of EditText with an Adapter ?

You will have an ArrayList which contain X EditText (according to your spinner number) and you create a "validate" methode which parse all arrayList and check if EditText is empty or not.

OBuiron
  • 176
  • 6
  • I don't see how to do your arraylist by the spinner, I know how to do in PHP, but I'm just beginning Android, can you show me an exemple ? –  Jul 07 '15 at 07:12
  • See this tutorial ( http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_example ) it's explain how to do an Adapter to link to a List. In your case you must add a custom constructor for first init and another method to update number (you update arraylist to get expected number and you call notifyDatasetChanged() it will update you list (ListView maybe) according to your updated arraylist). And finaly add a method validate which parse arraylist to check your edittext. – OBuiron Jul 07 '15 at 07:26
  • I will see it, thank you, for my problem i found a solution, i check if edittext is visible then i see if its not empty :) –  Jul 07 '15 at 08:02
0

you can use this as

if((mEditText.getText().toString()).equals("")){    

}else{    

}   
Andy
  • 49,085
  • 60
  • 166
  • 233
Ram Mandal
  • 1,899
  • 1
  • 20
  • 35
  • Yes but i have toi verify those are displayed –  Jul 07 '15 at 07:58
  • keep all the objects of edittext to the list and you can use loop to check if the edittext is visible or not and inside it check if it is empty or not – Ram Mandal Jul 07 '15 at 08:45