Get selected date from CalendarView in android

How to get entered date with day,month, year using CalendarView and set into TextView.

In this tutorial we are getting the user selected date form CalenderView in android activity. So here is the complete step by step tutorial for Get selected date from CalendarView in android.

android-project-download-code-button

How to Get selected date from CalendarView in android.

Code for MainActivity.java file.

 package com.getselecteddatefromcalendaview_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.TextView;

public class MainActivity extends Activity {

 CalendarView calender;
 TextView textview;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 calender = (CalendarView)findViewById(R.id.calendarView1);
 textview = (TextView)findViewById(R.id.textView1);
 
 calender.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
 
 @Override
 public void onSelectedDayChange(CalendarView view, int year, int month,
 int dayOfMonth) {
 // TODO Auto-generated method stub
 
 textview.setText("Date is : " + dayOfMonth +" / " + (month+1) + " / " + year);
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.getselecteddatefromcalendaview_android_examples.com.MainActivity" >

 <CalendarView
 android:id="@+id/calendarView1"
 android:layout_width="fill_parent"
 android:layout_height="300dp"
 android:layout_alignParentLeft="true"
 android:layout_alignParentTop="true"
 android:background="#03f0dc"
 
 />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/calendarView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="30dp"
 android:text="Date not Selected"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshots:

Get selected date from CalendarView in android

Click here to download Get selected date from CalendarView in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *