package com.example.appli_calendar03;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

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

public class MainActivity extends Activity
{

 CalendarView calenderview;
 TextView displaydata;

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
	 super.onCreate(savedInstanceState);
	 setContentView(R.layout.activity_main);
 
	 calenderview = (CalendarView)findViewById(R.id.calendarView1);
	 displaydata = (TextView)findViewById(R.id.textView1);
	 
	 init02();
	
	
	 
	
	 long selectedDate = calenderview.getDate(); // get selected date in milliseconds
 
	 calenderview.setOnDateChangeListener(new CalendarView.OnDateChangeListener()
	 {
 
		 @Override
		 public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) 
		 {
			 // TODO Auto-generated method stub
	  
			 displaydata.setText("Date: " + dayOfMonth + "/" + (month+1) + "/" + year);
		 }
	 });
 }
 
 
 public void initializeCalendar() 
 {
		
		// sets whether to show the week number.
	 calenderview.setShowWeekNumber(false);

		// sets the first day of week according to Calendar.
		// here we set Monday as the first day of the Calendar
	 calenderview.setFirstDayOfWeek(2);

		//The background color for the selected week.
	 calenderview.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green));
		
		//sets the color for the dates of an unfocused month. 
	 calenderview.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));
	
		//sets the color for the separator line between weeks.
	 calenderview.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));
		
		//sets the color for the vertical bar shown at the beginning and at the end of the selected date.
	 calenderview.setSelectedDateVerticalBar(R.color.darkgreen);
/*		
		//sets the listener to be notified upon selected date change.
	 calenderview.setOnDateChangeListener(new OnDateChangeListener() {
                    //show the selected date as a toast
			@Override
			public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
				Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
			}
		});
*/		
	}
 
 
 	public void init01()
 	{
 		  String date = "22/3/2014";
 		  String parts[] = date.split("/");

 		    int day = Integer.parseInt(parts[0]);
 		    int month = Integer.parseInt(parts[1]);
 		    int year = Integer.parseInt(parts[2]);

 		    Calendar calendar = Calendar.getInstance();
 		    calendar.set(Calendar.YEAR, year);
 		    calendar.set(Calendar.MONTH, month);
 		    calendar.set(Calendar.DAY_OF_MONTH, day);

 		    long milliTime = calendar.getTimeInMillis();
 		    calenderview.setDate (milliTime, true, true); 
 	}
 	
 	public void init02()
 	{
 		    
	    String selectedDate = "30/09/2016";
	    try {
			calenderview.setDate(new SimpleDateFormat("dd/MM/yyyy").parse(selectedDate).getTime(), true, true);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 
 	}
 	
 	
 

}