Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract date from CalendarView and display selected date?

Tags:

android

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);
like image 665
GuyWhoReadsStockoverflow Avatar asked Apr 26 '12 19:04

GuyWhoReadsStockoverflow


People also ask

How to get selected date from Calendar in Android?

Show activity on this post. Calendar date = Calendar. getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd"); curDate = sdf. format(date.

Which method is used to get a Calendar view in Android?

This article shows how to create an android application for displaying the Calendar using CalendarView. It also provides the selection of the current date and displaying the date. The setOnDateChangeListener Interface is used which provide onSelectedDayChange method.

Which method is used to get a Calendar view in Android Mcq?

1. getDate(): This method is used to get the selected date of CalendarView in milliseconds since January 1, 1970 00:00:00 in user's preferred time zone.


2 Answers

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
 });
like image 155
Snicolas Avatar answered Nov 12 '22 05:11

Snicolas


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.

like image 34
Ravi Avatar answered Nov 12 '22 04:11

Ravi