I have exactly 96 editText fields in my activity, what I want to know is how to validate that none is empty, in the case of one I have the following code, one of the two options:
String precio= this.precio.getText().toString();
if (precio.equals("")) {
Toast.makeText(this, "Ha dejado campos vacios",
Toast.LENGTH_LONG).show();
}
String precio= this.precio.getText().toString();
if (TextUtils.isEmpty(precio_coste) {
Toast.makeText(this, "Ha dejado campos vacios",
Toast.LENGTH_LONG).show();
}
How do I do it if I have to validate many fields at the same time. Thanks.
For now I am validating in the if with (or), but I want to know if there is a more simplified way:
if (dat_1.equals("")||dat_2.equals("")||dat_3.equals("")||dat_4.equals("")||dat_5.equals("")||dat_6.equals("")||dat_7.equals("")||dat_8.equals("")
||dat_9.equals("")||dat_10.equals("")||dat_11.equals("")||dat_12.equals("")||dat_13.equals("")||dat_14.equals("")||dat_15.equals("")||dat_16.equals("")
||dat_17.equals("")||dat_18.equals("")||dat_19.equals("")||dat_20.equals("")||dat_21.equals("")||dat_22.equals("")||dat_23.equals("")||dat_24.equals("")
||dat_25.equals("")||dat_26.equals("")||dat_27.equals("")||dat_28.equals("")||dat_29.equals("")||dat_30.equals("")||dat_31.equals("")||dat_32.equals("")
||dat_33.equals("")||dat_34.equals("")||dat_35.equals("")||dat_36.equals("")||dat_37.equals("")||dat_38.equals("")||dat_39.equals("")||dat_40.equals("")
||dat_41.equals("")||dat_42.equals("")||dat_43.equals("")||dat_44.equals("")||dat_45.equals("")||dat_46.equals("")||dat_47.equals("")||dat_48.equals("")
||dat_49.equals("")||dat_50.equals("")||dat_51.equals("")||dat_52.equals("")||dat_53.equals("")||dat_54.equals("")||dat_55.equals("")||dat_56.equals("")
||dat_57.equals("")||dat_58.equals("")||dat_59.equals("")||dat_60.equals("")||dat_61.equals("")||dat_62.equals("")||dat_63.equals("")||dat_64.equals("")
||dat_65.equals("")||dat_66.equals("")||dat_67.equals("")||dat_68.equals("")||dat_69.equals("")||dat_70.equals("")||dat_71.equals("")||dat_72.equals("")
||dat_73.equals("")||dat_74.equals("")||dat_75.equals("")||dat_76.equals("")||dat_77.equals("")||dat_78.equals("")||dat_79.equals("")||dat_80.equals("")
||dat_81.equals("")||dat_82.equals("")||dat_83.equals("")||dat_84.equals("")||dat_85.equals("")||dat_86.equals("")||dat_87.equals("")||dat_88.equals("")
||dat_89.equals("")||dat_90.equals("")||dat_91.equals("")||dat_92.equals("")||dat_93.equals("")||dat_94.equals("")||dat_95.equals("")||dat_96.equals("")
||dat_97.equals("")||dat_98.equals("")||dat_99.equals("")||dat_100.equals("")) {
Toast.makeText(PollaMundialista.this,"Faltan campos por llenar",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(PollaMundialista.this,"Todos los campos están llenos",Toast.LENGTH_SHORT).show();
}
To achieve this, you just have to get all the editTexts that your layout contains, go through them and evaluate their text. How to do it? easy.
First you get the layout that contains the editText.
Then with the
getChildCount()
layout method you get the number of the EditText that contains the layout. You will use this number to loop through the editText.Then iterate through the editText that the layout contains using a
for
. You get the editText that contains the layout with its methodgetChildAt()
.And finally you evaluate the text that each editText contains using a
if
. This way you will know if there are empty editText or not.The following example shows what this code would look like when implemented.
Note: Keep in mind that I have assumed that you have the editText inside a LinearLayout and that inside it you only have the editText. Despite this, this code can be easily adapted to any situation. Any questions you have leave it in the comments.
I am also encouraged. Well we have our
EditText
I store them in an array
We create a method to validate the fields, and it will return true if it finds any empty, and false if they are all filled
And as a last step, it would be to call the method when we want to do the validation.
I'll give you an example in its entirety. MainActivity