You see, I have the following code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
btnTranslate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String texto = textTranslate();
if (etBeforeTranslate.getText().toString().isEmpty()) {
ivLogo.setImageResource(R.drawable.logotipoangry);
Toast.makeText(MainActivity.this, "¡Ay que agobio! Intridici il tixti, anda", Toast.LENGTH_SHORT).show();
} else {
Translates = FirebaseDatabase.getInstance().getReference("Translates");
registerTranslates();
ivLogo.setImageResource(R.drawable.logotipo);
Intent intent = new Intent(MainActivity.this, TranslateActivity.class);
intent.putExtra("texto", texto);
startActivity(intent);
}
}
});
I have the problem in the conditional, that I verify that if the EditText is empty, I execute that code. The problem comes when I make a line break, although the line is empty, the application believes that there is text inserted, how can I tell it that even if the person presses enter without writing text, it is still empty and does not let him advance?
You can validate that it is empty even when it has blank spaces by doing a
trim()
In this case I suggest you restrict the
EditText
to a single line, defining these properties in theEditText
, it will not allow giving Enterand it will always remain on the same line, in this way no spaces would be inserted:Also consider adding validation for the case when spaces are inserted via the "space bar", using
.trim()
to remove the spaces.