Search code examples
androiddatelong-integercalendarview

Android - How can i check if calendar date has been selected


I'm making a task manager application where I require the following details:- name (string), description (string), date (long) and importance (int).

I am currently stuck on figuring out how to get the date and also see if date from calendarView has been selected.

TextView name, description, importance;
    CalendarView calendar;
    Button submit;
    ArrayList<Task> myTasks = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_task);

    name = (TextView)findViewById(R.id.CreateTaskNameET);
    description = (TextView)findViewById(R.id.CreateTaskDescriptionET);
    importance = (TextView)findViewById(R.id.CreateTaskImportanceET);
    calendar = (CalendarView) findViewById(R.id.CreateTaskCalender);

    submit = (Button)findViewById(R.id.CreateTaskSubmitBtn);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Task myTask = new Task(name.getText().toString(),
                    description.getText().toString(),
                    calendar.getDate(),
                    Integer.parseInt(importance.getText().toString()));

            myTasks.add(myTask);
        }
    });

}

public void validateRule(EditText name, EditText description, CalendarView calendar, EditText importance){
    if(name.getText().equals("")){

    }
    else if(description.getText().equals("")){

    }
    else if(calendar.getDate()){

    }
}

Once again, how can i determine if the date has been selected as it's one of my validation rules? Thanks


Solution

  • Please set a listener to the calendar view object which will allow you to create custom actions once the listener is triggered on a date change. OnDateChangeListener is a class method of calendar view object, more information can be accessed by referring to the code for CalendarView class.

    calendar.setOnDateChangeListener(new OnDateChangeListener() {
    
                @Override
                public void onSelectedDayChange(CalendarView view, int year, int month,
                        int dayOfMonth) {