Skip to content Skip to sidebar Skip to footer

How Can I Extract Date From Calendarview And Display Selected Date?

Currently I am using code below and I can get current date to be dispalyed in Text field. However I have no idea how to display selected date. I also used DatePicker and it display

Solution 1:

in onCreate :

CalendarViewv=newCalendarView( this );
 v.setOnDateChangeListener( newCalendarView.OnDateChangeListener() {
    publicvoidonSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
       this.calendar = newGregorianCalendar( year, month, dayOfMonth );
    }//met
 });

Solution 2:

CalendarViewview=newCalendarView(this);
setContentView(view);

view.setOnDateChangeListener(newOnDateChangeListener() {

    @OverridepublicvoidonSelectedDayChange(CalendarView arg0, int year, int month,
        int date) {
        Toast.makeText(getApplicationContext(),date+ "/"+month+"/"+year,4000).show();
    }
});

This will display the user selected date.

Solution 3:

Calendarc= Calendar.getInstance();

Calendar is different from CalendarView. CalendarView is a descendant of View so it has many event handlers, like an onClickListener. Perhaps this is what you are trying to find?

Solution 4:

CalendarViewview=newCalendarView(this);
setContentView(view);

view.setOnDateChangeListener(newOnDateChangeListener() {

    @OverridepublicvoidonSelectedDayChange(CalendarView arg0, int year, int month,
        int date) {
        Toast.makeText(getApplicationContext(),date+ "/"+month+"/"+year,4000).show();
    }
});

Solution 5:

Simple Kotlin code to get selected date from CalenderView:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><CalendarViewandroid:id="@+id/calendar_view"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Toast Selected Date" /></LinearLayout>

MainActivity.kt

import android.os.Bundle
import android.text.format.DateFormat
import android.util.Log
import android.widget.CalendarView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.testandcheck.databinding.ActivityMainBinding
import java.util.*

classMainActivity : AppCompatActivity() {

privatelateinitvar binding: ActivityMainBinding   // Binding object for ViewBinding.overridefunonCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    // Set date change listener on calenderView.// Callback notified when user select a date from CalenderView on UI.
    binding.calendarView.setOnDateChangeListener { calView: CalendarView, year: Int, month: Int, dayOfMonth: Int ->

        // Create calender object with which will have system date time.val calender: Calendar = Calendar.getInstance()

        // Set attributes in calender object as per selected date.
        calender.set(year, month, dayOfMonth)

        // Now set calenderView with this calender object to highlight selected date on UI.
        calView.setDate(calender.timeInMillis, true, true)
        Log.d("SelectedDate", "$dayOfMonth/${month + 1}/$year")
    }

    // Example button to show get selected date from CalenderView and show in Toast.
    binding.button.setOnClickListener {

        // Fetch long milliseconds from calenderView.val dateMillis: Long = binding.calendarView.date

        // Create Date object from milliseconds.val date: Date = Date(dateMillis)

        // Get Date values and created formatted string date to show in Toast.val selectedDayOfWeek = DateFormat.format("EEEE", date) as String // Mondayval selectedDay = DateFormat.format("dd", date) as String // 05val selectedMonthString = DateFormat.format("MMM", date) as String // Julval selectedMonthNumber = DateFormat.format("MM", date) as String // 6 --> Month Code as Jan = 0 till Dec = 11.val selectedYear = DateFormat.format("yyyy", date) as String // 2021val strFormattedSelectedDate = "$selectedDay-${selectedMonthNumber + 1}-$selectedYear ($selectedDayOfWeek)"
        Toast.makeText(applicationContext, "Selected Date = $strFormattedSelectedDate", Toast.LENGTH_SHORT).show()
    }
}

}

Note: Once you select any date on CalenderView it will just highlight that day but not select. Due to this reason you will always get system's current date-time when you call calendarView.date (kotlin) or calenderView.getDate() (Java). So you have to implement the setOnDateChangeListener. Upon receiving callback, you have to programmatically set the new Date object as shown.

Hope it helps...... Happy Coding...... :)

Post a Comment for "How Can I Extract Date From Calendarview And Display Selected Date?"