How to consider an empty EditText as "0"

3

I'm developing an Android app that calculates 3x3 arrays
so I have 18 EditText , however if I want to multiply an array 2x3, 2x2, 1x2, etc. I would have to fill the fields the size of the array and leave the other fields with 0 and this would not change the result.

I made a condition for it to notify if the field is empty but it would be better to consider the empty field as "0" without requiring the user to fill in all fields

    
asked by anonymous 28.09.2015 / 14:39

3 answers

3

This can be easily accomplished by declaring your EditText with its default value equal to zero and indicating that it can only receive numeric values:

<EditText
   android:id="@+id/edittext"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="0" 
   android:inputType="number"/>

The android: text="0" attribute assigns the value zero as default value.
The android: inputType="number" attribute causes only integer values to be accepted by EditText

    
28.09.2015 / 14:50
1

To get the value of your EditText , use this function by passing it as a parameter

private int getValorEdit(EditText edit){

    int ret = 0;

    if (! edit.getText().toString().equalsIgnoreCase("")) {
        ret = Integer.valueOf(edit.getText().toString());
    }   
    return ret;

}

Wait for help.

    
28.09.2015 / 14:42
1

None of the above corrections worked for me. Just this:

if(txtEdit.getText().toString().equals("")){
  num = 0;
} else {
  num = parseInt(txtEdit.getText().toString());
}
    
01.05.2018 / 06:06