0

Iam new to programming... still a beginner in Android...

I have a Login Activity, when I clikc submit it goes to another Activity. I have an EditBox which it enters. I want to detect if the EditBox is empty

If it is empty I want to avoid clicking a button which I am clicking using the etNumber.setOnFocusChangeListener(new View.OnFocusChangeListener()

This will save the data to a csv file...

The idea is that any empty editText will not be saved... and thus have empty lines in the csv...

3 Answers3

0

There are many ways to do it. But for the above problem statement the below code should work.

<Your EditText>.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(!editText.getText().toString().trim().isEmpty()){
         //Your code
    }
}
});
Kushal
  • 8,100
  • 9
  • 63
  • 82
  • Thank you this seemed to work.... Thank you very much.,.. I used .isEmpty, but I din't use .trim I guess I should have tried it.. – Peter Fraga Dec 29 '19 at 03:04
0
EditText editText  = (EditText) findViewById(R.id.edittext);
if(TextUtils.isEmpty(editText.getText().toString())) {
   Toast.makeText(MainActivity.this, "EditText is Empty", Toast.LENGTH_LONG).show();
 }
 else {
    Toast.makeText(MainActivity.this, "EditText is Not Empty", Toast.LENGTH_LONG).show();
 }
prakash
  • 109
  • 12
0

If you are using Kotlin (which you should) then below 1 line statement uses the kotlin inline extension to check for the emptiness,

editText.text.isNotEmpty().apply { 
    //do something
 }

here editText is id of your edit text , which you can get with below code,

val editText= findViewById(R.id.et_message) as EditText
AADProgramming
  • 6,077
  • 11
  • 38
  • 58