Disable a button according to the content of an arraylist

0

Good I'm doing an application in android studio, and in a part I create several dynamic edittext according to an intent of the previous screen, and I would like to do it is that the start button, which starts another intent, will only be enabled when it is checked that all the edidtext are not empty. Every time an edittext is created it is entered into an arrayList so I thought I would use the contains function but it was not as good as it would be.

 cant = Integer.parseInt(dato);

    ScrollView layout = (ScrollView) findViewById(R.id.linearJugadores);

    LinearLayout contenedor = new LinearLayout(this);
    contenedor.setOrientation(LinearLayout.VERTICAL);
    contenedor.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

    final ArrayList<EditText> listaEdit = new ArrayList<EditText>();
    for (int j = 1; j <= cant; j++ ){
        EditText edit = new EditText(this);
        TextView text = new TextView(this);
        edit.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        edit.setId(j);
        edit.setMinWidth(200);
        text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        text.setText("\nJugador " + j);
        text.setId(j);
        contenedor.addView(text);
        contenedor.addView(edit);
        listaEdit.add(edit);

    }
  Button bEmpezar = new Button(this);
      bEmpezar.setText("Empezar partida");
      bEmpezar.setBackgroundColor(R.color.colorNaranja);
      bEmpezar.setTextColor(R.color.colorBlanco);
      contenedor.addView(bEmpezar);
    
asked by Alejandro Moreno 30.08.2018 в 08:40
source

2 answers

1

You can do it in the following way because there are many fields it creates a method that validates for each item in the list if it is empty so it returns false in the example method:

This way you have to pass all the EditText as parameters  of the method, but it would not be the best since you had to manually add each EditText

public boolean validate(EditText... editTexts){

                                if (TextUtils.isEmpty(editTexts[0].getText())){
                                    editTexts[0].setError("Campo obligatorio");
                                    return false;
                                }

                                if (TextUtils.isEmpty(editTexts[1].getText())){
                                    editTexts[1].setError("Campo obligatorio");
                                    return false;
                                }

                                if (TextUtils.isEmpty(editTexts[2].getText())){
                                    editTexts[2].setError("Campo obligatorio");
                                    return false;
                                }

                                if (TextUtils.isEmpty(editTexts[3].getText())){
                                    editTexts[3].setError("Campo obligatorio");
                                    return false;
                                }

                                if (TextUtils.isEmpty(editTexts[4].getText())){
                                    editTexts[4].setError("Campo obligatorio");
                                    return false;
                                }
                                return true;
                            }

This would be the best way in your case:

public boolean validateAll(ArrayList<EditText> editTexts){

                            for (EditText current : editTexts){
                                if  (TextUtils.isEmpty(current.getText())){
                                 current.setError("Campo requerido");  
                                 return false;
                                } else
                                    return true;
                        }

For example you can show the button if the method returns true example:

if (validateAll ())    tu_boton.setVisible (View.VISIBLE);

This line shows it on the screen in case it is by default with the GONE property.

If you only want to deactivate the function but it shows on the screen then use the property:

tu_boton.setEnabled (true); // or false depending on the case

}

    
answered by 31.08.2018 / 16:53
source
0

You have to make conditions: if (EditTex! == null) { button.setVisibility (View.INVISIBLE); }

** INVISIBLE - as its name says it makes it invisible but it is still there, unlike the (View.GONE) it makes the item or in this case your button disappear completely.

    
answered by 31.08.2018 в 19:35