Tutorials  /  JavaScript

Mastering Calendar View in Android

Ccentron Redaktion · July 2024 ·2 min read ·JavaScript, Tutorial

Integrating a calendar widget into an Android application can be a crucial feature, especially when it comes to displaying and selecting dates. In this tutorial, we'll take a look at using the CalendarView class to implement and customize such a widget.

Embedding the Calendar Widget

To insert a CalendarView widget into the XML layout of our Android application, the following code is sufficient:

Code
<CalendarView
android:id="@+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

When running the application on a device, the current date is displayed by default. The CalendarView defaults to showing January 1, 1970. However, using the android:maxDate and android:minDate attributes, an individual time frame can be set. In Java, we use the setMaxDate() and setMinDate() methods, passing a long value to them.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Customizations and Event Handling

The display of the date and weekdays can be customized using the android:dateTextAppearance and android:weekTextAppearance attributes. Additionally, the CalendarView has an OnDateChangeListener, which is triggered every time the user selects a new date.

Code
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
    @Override
    public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
        // Event handling here
    }
});

Custom Adjustments and Functionalities

It's also possible to add custom styles and functionalities to the CalendarView. For this, corresponding styles need to be defined in the styles.xml file. In the MainActivity.java, various actions can then be implemented such as setting a custom range or changing the date with or without animation.

Conclusion

Integrating a CalendarView widget provides a user-friendly way to display and select dates in Android applications. By customizing styles and functionalities, the widget can be tailored to meet the requirements of the application.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren