-2

I replicate how to show datepicker popup from edittext from a question here(i use the accepted answer):

Datepicker: How to popup datepicker when click on edittext

but when im trying to validate if the EditText is not empty, it didnt work. Here is my attempt:

final Calendar myCalendar= Calendar.getInstance();
EditText tStart;
EditText tEnd;
TextInputLayout desc;
Editable tDesc;
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timeoff);

    tStart = (EditText) findViewById(R.id.startDate);
    tEnd = (EditText) findViewById(R.id.endDate);
    Button btnSubmit = (Button) findViewById(R.id.btnSubmit);

    DatePickerDialog.OnDateSetListener dateStart =new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH,month);
            myCalendar.set(Calendar.DAY_OF_MONTH,day);
            startupdateLabel();
        }
    };

    DatePickerDialog.OnDateSetListener dateEnd =new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH,month);
            myCalendar.set(Calendar.DAY_OF_MONTH,day);
            endupdateLabel();
        }
    };

    tStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            new DatePickerDialog(TimeoffActivity.this,dateStart,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

    tEnd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new DatePickerDialog(TimeoffActivity.this,dateEnd,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });
    
    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(tStart.toString().isEmpty() || TextUtils.isEmpty(tEnd.toString()) ){
                Toast.makeText(TimeoffActivity.this, "Please fill all field before submitting", Toast.LENGTH_LONG).show();
            } else{
                saveTimeoffRequest();
            }
        }
    });
}

I tried using 2 way but still didnt work:

tStart.toString().isEmpty() || TextUtils.isEmpty(tEnd.toString()) 

how to validate those 2 date EditText if its not empty?

thanks in advance.

Avocado
  • 29
  • 6
  • 1
    You should use `tStart.getText().toString()` to retrieve the text. `tStart.toString()` produces the default `View` toString which looks something like: `androidx.appcompat.widget.AppCompatTextView{1e38acf V.ED..... ......ID 0,0-0,0 #7f07006d app:id/main_text}` as an example. – Computable Sep 11 '22 at 18:31
  • @Gardener But later in `saveTimeoffRequest();` i want store tStart and tEnd as a timestamp in firebase. do I need to parse it later in the next method? – Avocado Sep 11 '22 at 18:58

1 Answers1

-1

Try This

Change

btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(tStart.toString().isEmpty() || TextUtils.isEmpty(tEnd.toString()) ){
                Toast.makeText(TimeoffActivity.this, "Please fill all field before submitting", Toast.LENGTH_LONG).show();
            } else{
                saveTimeoffRequest();
            }
        }
    });

To

btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(tStart.getText().toString().trim().length() <= 0 || tEnd.getText().toString().trim().length() <= 0){
                    Toast.makeText(MainActivity.this, "Please fill all field before submitting", Toast.LENGTH_LONG).show();
                } else{
                    Log.e("check", "do something....");
                }
            }
        });
Mayur
  • 332
  • 1
  • 8