Reputation: 517
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 displays selected date fine, however I would prefer CalendarView. Thanks
Calendar c = Calendar.getInstance();
SimpleDateFormat ss = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
String currentdate= ss.format(date);
TextView editText = (TextView) this.findViewById(R.id.textfield1);
editText.setText(currentdate);
Upvotes: 6
Views: 36288
Reputation: 3460
Simple Kotlin
code to get selected date from CalenderView
:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CalendarView
android:id="@+id/calendar_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android: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.*
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding // Binding object for ViewBinding.
override fun onCreate(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 // Monday
val selectedDay = DateFormat.format("dd", date) as String // 05
val selectedMonthString = DateFormat.format("MMM", date) as String // Jul
val selectedMonthNumber = DateFormat.format("MM", date) as String // 6 --> Month Code as Jan = 0 till Dec = 11.
val selectedYear = DateFormat.format("yyyy", date) as String // 2021
val 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 callcalendarView.date
(kotlin) orcalenderView.getDate()
(Java). So you have to implement thesetOnDateChangeListener
. Upon receiving callback, you have to programmatically set the newDate
object as shown.
Hope it helps...... Happy Coding...... :)
Upvotes: 0
Reputation: 35
Calendar c = Calendar.getInstance();
SimpleDateFormat ss = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
String currentdate= ss.format(date);
TextView editText = (TextView) this.findViewById(R.id.textfield1);
editText.setText(currentdate);
above one is ur code try below code
CalendarView cv = (CalendarView)findViewById(R.id.cv1);
Calendar c = Calendar.getInstance();
SimpleDateFormat ss = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date(cv);
String currentdate= ss.format(date);
TextView editText = (TextView) this.findViewById(R.id.textfield1);
editText.setText(currentdate);
Upvotes: -1
Reputation: 51
CalendarView view = new CalendarView(this);
setContentView(view);
view.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView arg0, int year, int month,
int date) {
Toast.makeText(getApplicationContext(),date+ "/"+month+"/"+year,4000).show();
}
});
Upvotes: 0
Reputation: 31
CalendarView view = new CalendarView(this);
setContentView(view);
view.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView arg0, int year, int month,
int date) {
Toast.makeText(getApplicationContext(),date+ "/"+month+"/"+year,4000).show();
}
});
This will display the user selected date.
Upvotes: 3
Reputation: 38168
in onCreate :
CalendarView v = new CalendarView( this );
v.setOnDateChangeListener( new CalendarView.OnDateChangeListener() {
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
this.calendar = new GregorianCalendar( year, month, dayOfMonth );
}//met
});
Upvotes: 5
Reputation: 86958
Calendar c = 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?
Upvotes: 0