As a condition if an EditText is empty or not within an Intent.EXTRA_TEXT on Android?

1

I have a function that sends an email, the data that goes inside the mail are loaded by the user in the app, they are a series of fields of EditText , now what I need to do is within this Intent.EXTRA_TEXT condition if a field is empty at the time of sending the email, in that field nothing goes, instead if the user charges some value, that value goes; I have it this way right now

intent.putExtra(Intent.EXTRA_EMAIL, TO);
    intent.putExtra(Intent.EXTRA_SUBJECT, "Relevamiento");
    intent.putExtra(Intent.EXTRA_TEXT,

            Html.fromHtml(new StringBuilder()
                    .append("<p><b>Fecha:</b>" + fecha.getText() + "</p>")
                    .append("<p><b>Obra:</b>" + obra.getText() + "</p>")
                    .append("<p><b>Ancho:</b>" + ancho2.getText() + "</p>")
                    .append("<p><bold>Observacion:</bold>" + obs2.getText() + "</p>")
                    .toString()


            )

What I want to do in synthesis is; if the EditText is empty, the .append is simply (""); if it is not empty, the .append is

 ("<p>Titulo del campo:" + editText.getText + "</p>"). 

Any ideas? Inside the EXTRA_TEXT does not let me perform a condition if , I tried creating a separate function that does the validation, but I get an error if I have it this way:

  (Intent.EXTRA_TEXT, ValidacionCampos()
    
asked by Harles Pereira 13.03.2018 в 18:02
source

2 answers

0

It can be done in this way:

if(!fecha.getText().toString().trim().equals(""){

     //Agrega texto si el contenido no es "".

}else{

     //No agrega texto si el contenido es "".
}

Applying the above can be done using a ternary operation:

.append(!fecha.getText().toString().trim().equals("") ? "<p><b>Fecha:</b>" + fecha.getText() + "</p>" : "")

This would be the complete code:

Html.fromHtml(
                new StringBuilder()
                    .append(!fecha.getText().toString().trim().equals("")?"<p><b>Fecha:</b>" + fecha.getText() + "</p>" : "")
                    .append(!obra.getText().toString().trim().equals("")?"<p><b>Obra:</b>" + obra.getText() + "</p>" : "")
                    .append(!ancho2.getText().toString().trim().equals("")?"<p><b>Ancho:</b>" + ancho2.getText() + "</p>" : "")
                    .append(!obs2.getText().toString().trim().equals("")?"<p><bold>Observacion:</bold>" + obs2.getText() + "</p>" : "")
                    .toString()
};
    
answered by 13.03.2018 / 18:09
source
1

It is not necessary that you write everything in a single sentence:

intent.putExtra(Intent.EXTRA_TEXT,
        Html.fromHtml(new StringBuilder()
                .append("<p><b>Fecha:</b>" + fecha.getText() + "</p>")
                .append("<p><b>Obra:</b>" + obra.getText() + "</p>")
                .append("<p><b>Ancho:</b>" + ancho2.getText() + "</p>")
                .append("<p><bold>Observacion:</bold>" + obs2.getText() + "</p>")
                .toString()
        ));

It is also bad practice to concatenate String , especially since you were already using StringBuilder , you can perfectly make it in several sentences for your conditional:

StringBuilder builder = new StringBuilder()
            .append("<p><b>Fecha:</b>").append(fecha.getText()).append("</p>")
            .append("<p><b>Obra:</b>").append(obra.getText()).append("</p>")
            .append("<p><b>Ancho:</b>").append(ancho2.getText()).append("</p>")
            .append("<p><bold>Observacion:</bold>").append(obs2.getText()).append("</p>");
if(!editText.getText().toString().isEmpty()) {
    builder.append("<p>Titulo del campo:").append(editText.getText()).append("</p>");
}
intent.putExtra(Intent.EXTRA_TEXT,
        Html.fromHtml(builder.toString()));
    
answered by 13.03.2018 в 19:59