Java Code Examples for android.widget.CalendarView#setOnDateChangeListener()

The following examples show how to use android.widget.CalendarView#setOnDateChangeListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From privacy-friendly-food-tracker with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    calendarView = (CalendarView) findViewById(R.id.CalendarView); // get the reference of CalendarView
    calendarView.setDate(System.currentTimeMillis(), false, true);
    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
            Intent intent = new Intent(MainActivity.this, OverviewActivity.class);
            // Build string from chosen date to parse into Date object
            // (month+1) because months count from 0 in java but SimpleDateFormat parses it as 1-12
            String chosenDate = dayOfMonth + "/" + (month+1) + "/" + year;
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            try{
                Date date = sdf.parse(chosenDate);
                long millis = date.getTime();
                intent.putExtra("DATE", millis);
            } catch (ParseException e){
                e.printStackTrace();
            }

            startActivity(intent);
        }
    });
    overridePendingTransition(0, 0);
}