I am displaying a date in a TextView
. It is all working fine when there is some date. But if no date is chosen or if the TextView
is empty (there is a hint "dd-mm-yyyy" though) then the app crashes. I am checking for empty TextView
as follows: if(textview.setText().toString().isEmpty()) {//show error}
can anyone help what I am doing wrong?
Initialisation of TextView
and TextInputlayout
:
tv_Current_Date = (TextView) findViewById(R.id.tv_Current_Date);
til_Current_Date = (TextInputLayout) findViewById(R.id.til_Current_Date);
Here is the code responsible for crash:
if (tv_Current_Date.getText().toString().isEmpty()) {
til_Current_Date.setError("Please choose a date");
}
Method to set date:
public void setCurrentDateOnView() {
String dateFormat = "dd-MM-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US);
tv_Current_Date = (TextView) findViewById(R.id.tv_Current_Date);
tv_Current_Date.setText(simpleDateFormat.format(calendar_now.getTime()));
String short_weekday = new DateFormatSymbols().getShortWeekdays()[day_of_current_week];
tv_Current_weekday.setText(short_weekday);
}