SundeepK / CompactCalendarView

An android library which provides a compact calendar view much like the one used in google calenders.
MIT License
1.52k stars 427 forks source link

Current day not displayed on activity load #199

Closed AerWyn81 closed 7 years ago

AerWyn81 commented 7 years ago

Hi,

Sorry by advance, english isn't my natural language and I'm new!

So, my problem is when I call another activity with your calendar implemented, I need to touch the calendar to display the selected current day. I currently add only the xml and not other (except findViewById programmatically).

I wanted to display the current day selected when the activity load, it's possible? Did I make a mistake or I forgot something?

Thanks by advance! AerWyn81

SundeepK commented 7 years ago

Hi, so you have an existing activity and when a user clicks a button a new activity opens with calendar? Can you tell me exactly how you open the calendar so that I can have a go?

In any case, you can call the invalidate() method to redraw the calendar onResume() and onCreate() though it shouldn't be necessary. Give that a go, its a little inconvenient but it should work. All views have invalidate which basically means redraw the calendar. If that doesn't work you can also just set the current date as well. Mean while I can try debugging things to see why it is not automatically set.

AerWyn81 commented 7 years ago

Hi,

Thanks for your fast reply.

So, yes when a user clicks a button a new activity opens with calendar. Currently, there is no code to show the calendar, I just implemented it in xml and displayed it thanks to setContentView(layout) in the onCreate().

In my code :

private CompactCalendarView calendar;
calendar = (CompactCalendarView) findViewById(R.id.iiuh_calendar);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setUseThreeLetterAbbreviation(true);

I tried to add calendar.invalide() in onCreate() but nothing has changed. The invalide() method doesn't seems to refresh the calendar, I always need to touch the calendar a first time for trigger the selection of the current day OR to show events after being hide. Did I miss something?


Other thing, if possible or if you can, can you add a method to unselect a date witch selected before (not current day, an other) ?

AerWyn81

SundeepK commented 7 years ago

Did you try to call the setCurrentDate() method as well onCreate and onResume methods? That will select the current date programatically.

There is also a problem with events displaying? It would be great if you can upload an example project of what you currently have? Then I can have a look to debugg things further.

AerWyn81 commented 7 years ago

Hi,

Yep I tried but nothing has changed. Here an example of my code :

private ViewFlipper viewFlipper;
private CompactCalendarView calendarView;
private Button button;
private long mLastClickTime = 0;
private SimpleDateFormat date = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());

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

    initViews();

    button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (SystemClock.elapsedRealtime() - mLastClickTime < 500) { //prevent from spamTouching
            }
            else {
                mLastClickTime = SystemClock.elapsedRealtime();
                viewFlipper.showNext(); //show calendar
                calendarView.setCurrentDate(Calendar.getInstance().getTime()); //I don't know what to put inside setCurrentDate(), a date, yeah, but.. ?
                calendarView.invalidate(); //refresh calendar

                //test event
                try {
                    Event ev1 = new Event(Color.BLUE,date.parse("17-05-2017").getTime()); //french locale
                    calendarView.addEvent(ev1,true);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            return false;
        }
    });

}

//init views
public void initViews()
{
    viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
    calendarView = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
    button = (Button) findViewById(R.id.button);
}

@Override
protected void onResume() {
    super.onResume();
}

XML : <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="calendar"/>

<ViewFlipper
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/button"
    android:id="@+id/viewflipper">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hi"/>

    <com.github.sundeepk.compactcalendarview.CompactCalendarView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/compactcalendar_view"
        android:layout_width="368dp"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:layout_height="250dp"
        app:compactCalendarTargetHeight="250dp"
        app:compactCalendarTextSize="12sp"
        app:compactCalendarBackgroundColor="#ffe95451"
        app:compactCalendarTextColor="#fff"
        app:compactCalendarCurrentSelectedDayBackgroundColor="@android:color/transparent"
        app:compactCalendarCurrentDayBackgroundColor="#B71C1C"
        app:compactCalendarMultiEventIndicatorColor="#fff"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp"/>
</ViewFlipper>

When I show the calendar, as you will see, you will need to touch 1 time the calendar for display the currentDay. What is surprising, if I add an event, for example tomorrow, the event is displayed, don't need the first touch.

Thank you for your patience :) AerWyn81

SundeepK commented 7 years ago

Umm ok I see your using ViewFlipper not tested with this before. That might be causing some problem. Will need to investigate, though it does seem a little odd it would have an effect on such a specific thing. I would suggest to see if it works in another layout type like relative layout as a test. I'll try and have a look some time tomorrow to see if I can replicate the problem.

Thanks for the help so far.

AerWyn81 commented 7 years ago

Thanks!

I just tried with a ViewSwitcher, RelativeLayout and the problem reappeared. For RelativeLayout, my test was to use the method hideCalendar() to hide the calendar on the onCreate() and I show it back after touching the button. I always need to touch the calendar for display the selected current day.

AerWyn81

SundeepK commented 7 years ago

Can you comment out the hideCalendar() call in onCreate()? Does it work? If it works, I would suggest replacing the hideCalendar() with using a tree observer like this:

https://github.com/SundeepK/CompactCalendarView/blob/c006a769bdaad74f96a126189745e16d72d2db84/sample/src/main/java/sundeepk/github/com/sample/CompactCalendarTab.java#L174

Instead of showCalendarWithAnimation, you want to hide. But the rest of the code will be the same. So now the calendar will auto hide onCreate and then when you click button you can show again. Let me know if this works for you.

AerWyn81 commented 7 years ago

Hi,

Can you comment out the hideCalendar() call in onCreate()? Does it work?

image Is that what you asked me? Nope, that doesn't work.

On activity load : image

After activity load -> touching calendar : image

After activity load -> touching calendar -> click on the button (adding an event) : image

After activity load -> touching calendar -> click on the button -> retouching calendar : image

Do you understand?

Thanks for your patience!

AerWyn81

SundeepK commented 7 years ago

Thanks, I'll try and create a demo project with the code and layout your provided earlier and see how I can reproduce the problem. Would be a plus if you already have a demo app that I can launch directly?

AerWyn81 commented 7 years ago

Hi,

Of course, you can find a demo CalendarTest here : [Removed - The author replied] If you need other things, tell me.

AerWyn81

SundeepK commented 7 years ago

Ok so I just realised that you have set: app:compactCalendarCurrentSelectedDayBackgroundColor="@android:color/transparent". This means that the current selected day color will be transparent. This is why you are not seeing anything selected onCreate(). If you change the color to black for example: app:compactCalendarCurrentSelectedDayBackgroundColor="#000" you will see the today with a black color selected. Select another day and you will see that day selected in black. This will leave the current day showing instead of the black color.

The current selected day color takes priority over the current day color and because its transparent, it colors over the current day color. I hope that makes sense.

AerWyn81 commented 7 years ago

Hi,

Ok I understood. That did the trick. Wow sorry.. That was stupid xD. Ah.. that is a problem for what I want to do.. Is there a solution to "remove" or bypass compactCalendarCurrentSelectedDayBackgroundColor? This is why I set it transparent because in my app, I don't need to display the current selected day background :/.

Thanks you so much!

AerWyn81

SundeepK commented 7 years ago

Sorry for the late delay, but yes there might be a work around.

Use thise property: https://github.com/SundeepK/CompactCalendarView/blob/master/library/src/main/res/values/attrs.xml#L30 in the xml.

Just set compactCalendarShouldSelectFirstDayOfMonthOnScroll to false to prevent it to automatically select the first day of the month on scroll. Keep the current selected day as transparent color. This might just work but I haven't tested it.

If it doesn't then you can easily clone the code and import the libaray to your project and simple delete one line of code to acheive what you want.

Simply need to remove the if condition and 3 lines after that: https://github.com/SundeepK/CompactCalendarView/blob/master/library/src/main/java/com/github/sundeepk/compactcalendarview/CompactCalendarController.java#L883

That if condition checks for a current selected day.

Hope that help.

AerWyn81 commented 7 years ago

Hi,

It's ok!

Just set compactCalendarShouldSelectFirstDayOfMonthOnScroll to false to prevent it to automatically select the first day of the month on scroll. Keep the current selected day as transparent color. This might just work but I haven't tested it.

I tried already this thing and this doesn't work : set compactCalendarShouldSelectFirstDayOfMonthOnScroll to false + color.transparent. As you said before, the transparent color take priority on currentDayBackground. I think when calendar load, the compactCalendarCurrentDayBackgroundColor is set, and after, above, the compactCalendarCurrentSelectedDayBackgroundColor that's why the current day isn't selected with transparent color, and if I touch an other day, the compactCalendarCurrentSelectedDayBackgroundColor change for this day (normal) and display me correctly the compactCalendarCurrentDayBackgroundColor because that remove the transparent shape. Wow I don't know if you will understand here, that was a very bad english syntax.

I will try your second method tomorrow and tell you if that work!

Thanks, AerWyn81

AerWyn81 commented 7 years ago

Hi,

Ok, so, with your trick, I removed the fact where a user could select a date. That's a great job ! But.. When calendar load, it doesn't display the current selected day background.

AerWyn81

SundeepK commented 7 years ago

Ok, I'll create a branch for you with the required changes. Then you can simply clone the project and use it in yuor project. Maybe I missed something. But this will solve the problem for you. I will let you know once I made the required changes.

AerWyn81 commented 7 years ago

Hi,

Wow thanks for your investment and patience.

AerWyn81

SundeepK commented 7 years ago

So I created a branch @help/#199_disable_selectable_day. Should be able to git fetch and then switch to branch. Or go here: https://github.com/SundeepK/CompactCalendarView/tree/%40help/%23199_disable_selectable_day

Basically, now the selectable day should be removed and the only day that is highlighted is the current day. Is that what you wanted? If not then left me know. I have some time over the next day to solve this issue.

AerWyn81 commented 7 years ago

Hi,

That's exactly what I wanted ! So much thanks for your time ! That helped me so much ! A last thing.. (sorry)

As you can see, there 3 events for these 3 days. image

When I select a date with event : image

The dot (for an event you know) below the date goes below. I guess it's because the date is still highlighted but just not show, right? Or it's a problem with my code? I didn't touch other things since my last code. This is a little little thing, but I'm maniac.., if you can't fix it, it does not matter, you do so much for me ! Thanks :)

AerWyn81

SundeepK commented 7 years ago

Do you want to disable selecting days completely? So user can only see the calendar and events. And when user selects a day, nothing is selected? I made another change, do a git pull from the branch. Basically I commented a line which handles a user clicking the calendar. Hope this solves the problem?

AerWyn81 commented 7 years ago

Hi,

Wow I'm bad in english, ahah, nope, do you see the dot below 18 ? It is lower when selected. It's possible to let the dot at the same height? Sorry.

AerWyn81

SundeepK commented 7 years ago

Do you have shouldDrawIndicatorsBelowSelectedDays() method called on the calendar any where? This method allows you to draw the selected day highlight and then draw the dot below it like you have shown. This makes sense if you have a color for selected day. Having transparent will show like yours.

Why not disable selecting all together on click? The user cant see the selected day anyway. So if you use the latest code I have provided it disables on click and will solve the problem as well. And you will also see all your events. So the latest code I have committed on the branch will show all events but you just can't tap and highlight any days.

Or do you want transparent selected day. Where the user is able to select the event, but not able to see the current selected day, but still receive a callback in your code?

I hope that makes sense?

SundeepK commented 7 years ago

I made some more changes. This covers the case where by if you want transparent color and also still want to select days and it will always show the event on a selected day. Try a git pull again, hope that helps.

AerWyn81 commented 7 years ago

Hi,

Do you have shouldDrawIndicatorsBelowSelectedDays() method called on the calendar any where? This method allows you to draw the selected day highlight and then draw the dot below it like you have shown. This makes sense if you have a color for selected day. Having transparent will show like yours.

Yep, I had write this. I commented this line, and now when I select a date with event, the dot disappears. I want to show it even if the day was clicked, (when user come back on the calendar).

Why not disable selecting all together on click? The user cant see the selected day anyway. So if you use the latest code I have provided it disables on click and will solve the problem as well. And you will also see all your events. So the latest code I have committed on the branch will show all events but you just can't tap and highlight any days.

Yep but I need to trigger an event when user click on a date with event.

Or do you want transparent selected day. Where the user is able to select the event, but not able to see the current selected day, but still receive a callback in your code?

All is fine actually! I just want try to fix this dot below date with event, just because I'm maniac.. Because when I click on a date, this dot is not at the same height compared to the others, as you can see on my lasts screenhots.

Hope you understand my bad english..

AerWyn81

SundeepK commented 7 years ago

I made some more changes here:

https://github.com/SundeepK/CompactCalendarView/tree/%40help/%23199_disable_selectable_day

Please try it and git it a go.

AerWyn81 commented 7 years ago

Hi,

That's all ! You did all, thanks you for your patience and all! You are a really great developer and this project, a good adaptable calendar!

Thanks !!! AerWyn81