Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codename One - Custom Calendar day button

My app saves appointments and shows them on a Multibutton list, but I also have created a screen with a Calendar using the GUI builder, because I want to create a "calendar view" so the user can take a look at the calendar and:

1- see every date that has at least one appointment highlighted (custom UIID)

2- see on each calendar day, the number of appointments he has for that day, along with the date of course.

I read I can do this by overriding updateButtonDayDate() and createDay() but I don't know how to do that in my code. Could you show me an example?

enter image description here

like image 290
Felipe Avatar asked Dec 06 '25 05:12

Felipe


1 Answers

For you to override updateButtonDayDate() and createDay() in the Calendar class, you need to create a custom calendar that extends codenameone calendar (Make sure your extend com.codename1.ui.calendar and not java.util).

You will have to scrap GUI builder calendar and create one in code after creating your custom calendar class. Check the usage example that follows the code below:

import com.codename1.ui.Button;
import com.codename1.ui.Calendar;
import static com.codename1.ui.Component.CENTER;

public class CustomCalendar extends Calendar {

    @Override
    protected Button createDay() {
        //Customize your button here
        Button day = new Button();
        day.setAlignment(CENTER);
        day.setUIID("CalendarDay");
        day.setEndsWith3Points(false);
        day.setTickerEnabled(false);
        return day;
    }

    @Override
    protected void updateButtonDayDate(Button dayButton, int currentMonth, int day) {
        //Customize day values 
        dayButton.setText("" + day);
    }
}

And to use it, declare a new calendar based on your custom version:

CustomCalendar myCalendar = new CustomCalendar();
myContainer.add(myCalendar);

You can try all sort of things in your CustomCalendar until you find something that works. Ctrl + click on Calendar to check the source and know what you can override and what you can't.

You can even create your version of Calendar that extends Container (although not advisable).

like image 153
Diamond Avatar answered Dec 08 '25 18:12

Diamond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!