CalendarView today date item click

18,460

Solution 1

setOnDateChangeListener will not fire, but OnGlobalLayoutListener will. So if you try:

calendarView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
    @Override
    public void onGlobalLayout(){
    }
});

Solution 2

A review of CalendarView.java indicates that the highlighted row is overrided by an internal private class which consumes the touch event, so neither OnClickListener() nor OnTouchListener() will work.

The first (harder) options is to make your own copy of CalendarView.java (call it MyCustomCalendarView.java) and include it into your project. Within MyCustomCalendarView.java, add code to register an OnTouchListener like so:

private View.OnTouchListener touchListener;
@Override
public void setOnTouchListener( View.OnTouchListener otl) {
    touchListener = otl;
}

Then in the private class WeeksAdapter's OnTouch() method, add this line:

@Override
 public boolean onTouch(View v, MotionEvent event) {
     if (mListView.isEnabled() && mGestureDetector.onTouchEvent(event)) {
         touchListener.onTouch(v, event);
     ...

Then MyCustomCalendarView could be used as follows:

MyCustomCalendarView thisCalendar = (MyCustomCalendarView) rootView.findViewById(R.id.mycustom_calendar_view_layout_id);
thisCalendar.setOnTouchListener(myCustomListener);

Another (easier) alternative is to add a button to the calendarView layout, and use the button's onClickListener (plus maybe a flag variable) to detect whether any date has been selected. In this case, selecting the current date will be the same as "no date selected" from the device's point of view, so you can use that fact to write in today's date, or other custom actions.

You can use datetimepicker-library's date picker layout to help you get started:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:gravity="center" 
    android:orientation="vertical" 
    android:background="@color/date_picker_view_animator" 
    android:layout_width="@dimen/date_picker_component_width" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="wrap_content" 
        android:layout_height="@dimen/selected_calendar_layout_height">
        <include layout="@layout/date_picker_selected_date" />
    </LinearLayout>

    <!-- this is the calendarView, add your own version here as you see fit -->
    <include layout="@layout/date_picker_view_animator" />

    <!-- this is the button to be added to this layout -->
    <View 
        android:background="@color/line_background" 
        android:layout_width="@dimen/date_picker_component_width" 
        android:layout_height="1.0dip" />
        <include layout="@layout/date_picker_done_button" />
</LinearLayout>

Solution 3

Try to use next way:

Date nowDate = new Date();

int mYear = nowDate.getYear();
int mMonth = nowDate.getMonth();
int mDay = nowDate.getDay();

CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView1);
calendarView.setOnDateChangeListener(new OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month,
        int dayOfMonth) {

        mYear = year;
        mMonth = month;
        mDay = dayOfMonth;

        openGlavnaAktivnost();
    }
});


private void openGlavnaAktivnost(){

    Intent k = new Intent(GlavnaAktivnost.this, DatumDetalji.class);
    k.putExtra("godina", mYear);
    k.putExtra("mesec", mMonth);
    k.putExtra("dan", mDay);
    startActivity(k);
}

Solution 4

Try this with below way:

//First set current day,month,year :

Calendar c = Calendar.getInstance();
int prevDay = c.get(Calendar.DAY_OF_MONTH);
int prevMonth = c.get(Calendar.MONTH);
int prevYear = c.get(Calendar.YEAR);
Date d = new Date(prevYear, prevMonth, prevDay);
c.setTime(d);

Then use dateChangeListener :

CalendarView calendar = (CalendarView) findViewById(R.id.calendarViewCN);
calendar.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year,int month, int dayOfMonth) {
<b>boolean changed = changeUpdate(year, month, dayOfMonth);</b>
if (changed) {
Toast.makeText(getApplicationContext(),
"New Y: " + year + " M: " + month + " D: "+dayOfMonth, Toast.LENGTH_LONG).show();
}
}
});

Now declare the changeUpdate() :

private boolean changeUpdate(int curYear, int curMonth, int curDay) {
boolean changed = false;
if (curDay != prevDay || curMonth!=prevMonth || curYear!=prevYear) {
changed = true;
}

Solution 5

Not a direct solution but what I finally did was to ignore OnDateChangeListener and add a SELECT button. Not my desired solution either but it works...

https://stackoverflow.com/a/33635580/1121497

Share:
18,460

Related videos on Youtube

Aleksa
Author by

Aleksa

Updated on July 04, 2022

Comments

  • Aleksa
    Aleksa about 2 years

    I searched a lot on the internet and I couldn't succeed to find correct solution for CalendarView click on today date.

    enter image description here

    I need to use CalendarView for events app. setOnDateChangeListener works good for other day clicks.

    Code:

    CalendarView calendarView=(CalendarView) findViewById(R.id.calendarView1);
        calendarView.setOnDateChangeListener(new OnDateChangeListener() {
    
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month,
                    int dayOfMonth) {
                Intent k = new Intent(GlavnaAktivnost.this, DatumDetalji.class);
                k.putExtra("godina", year);
                k.putExtra("mesec", month);
                k.putExtra("dan", dayOfMonth);
                startActivity(k);
    
            }
        });
    

    I dont understand well what I need to do and what is the best solution for this? Answers that I was abled to find on stack is to extend CalendarView or bind click on UI but I couldn't find a way with it. Can you give me some example?

    Tnx.