Wednesday 17 August 2016

CalenderView


This class is a calendar widget for displaying and selecting dates. The range of dates supported by this calendar is configurable. A user can select a date by taping on it and can scroll and fling the calendar to a desired date.
http://i.stack.imgur.com/zloKv.png

Simple XML Example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/LinearLayout1"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
android:background="#FF550D"
   
>
<
CalendarView
   
android:id="@+id/calender1"
   
android:layout_width="fill_parent"
   
android:layout_height="wrap_content"></CalendarView>
</
LinearLayout>


XML Attribute
XML attributes
android:dateTextAppearance   
The text appearance for the day numbers in the calendar grid.
android:dateTextAppearance="@android:style/TextAppearance.Large"
or
android:dateTextAppearance="?android:attr/textAppearanceLarge"

Some Other style
@android:style/TextAppearance.Large
?android:attr/textAppearanceLarge
TextAppearance.Small          
textAppearanceSmall
TextAppearance.Medium     
textAppearanceMedium
TextAppearance.Large.Inverse             
textAppearanceLargeInverse

android:firstDayOfWeek            
The first day of week according to Calendar.
android:firstDayOfWeek="2"

android:focusedMonthDateColor           
This is deprecated. The color for the dates of the focused month.
android:focusedMonthDateColor="@android:color/darker_gray"

May be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".
May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
This corresponds to the global attribute resource symbol focusedMonthDateColor.

android:maxDate           
The minimal date shown by this calendar view in mm/dd/yyyy format.
android:maxDate="06/25/2016"

android:minDate            
The minimal date shown by this calendar view in mm/dd/yyyy format.
android:minDate="06/10/2016"


android:weekDayTextAppearance         
The text appearance for the week day abbreviation in the calendar header.
android:dateTextAppearance="@android:style/TextAppearance.Large"
or
android:dateTextAppearance="?android:attr/textAppearanceLarge"

Some Other style
@android:style/TextAppearance.Large
?android:attr/textAppearanceLarge
TextAppearance.Small          
textAppearanceSmall
TextAppearance.Medium     
textAppearanceMedium
TextAppearance.Large.Inverse             
textAppearanceLargeInverse


Important Methods Of Calendar View: need work
Let’s we discuss some important methods of Calendar View that may be called in order to manage the CalendarView.
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. This method returns long type value for selected date.
A). Return date in raw format - Below we get the selected of CalendarView in milliseconds.
long sdate = calendarView1.getDate();
 
B). Return formatted data
 
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

String sdate = sdf.format(new Date(calendarView1.getDate()));
 
2. setDate(long date):
This method is used to set the selected date in milliseconds since January 1, 1970 00:00:00 in user’s preferred  time zone.
A). Set date from milli second value - Below we set the selected date in milliseconds for a CalendarView.
calendarView1.setDate(1463918166920L);
B). Set data from formatted value
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();

calendarView1.setDate (milliTime, true, true);

3. setFirstDayOfWeek(int firstDayOfWeek):
 This method is used to set the first day of the week.
Below we set the 2 value means Monday as the first day of the week.
calendarView1.setFirstDayOfWeek(2);
 // set Monday as the first day of the week

4. getFirstDayOfWeek(): 
This method is used to get the first day of week. This method returns an int type value.
Below we get the first day of the week of CalendarView.
int firstday = calendarView1.getFirstDayOfWeek();
 5. setMaxDate(long maxDate): 
This method is used to set the maximal date supported by thisCalendarView in milliseconds since January 1, 1970 00:00:00 in user’s preferred time zone.
Below we set the long value for maximal date supported by the CalendarView.
A). Set date from milli second value - Below we set the selected date in milliseconds for a CalendarView.
calendarView1. setMaxDate (1463918166920L);
B). Set data from formatted value
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();

calendarView1. setMaxDate (milliTime, true, true);

6. getMaxDate():
This method is used to get the maximal date supported by this CalendarView in milliseconds since January 1, 1970 00:00:00 in user’s preferred time zone. This method returns long type value for maximal date supported by this CalendarView.
Below we firstly set the long value for the maximal date and then get the maximal value supported by the CalendarView.
long getmaxdate = calendarView1.getMaxDate();
 7. setMinDate(long minDate):
 This method is used to to set the minimal date supported by this CalendarView in milliseconds since January 1, 1970 00:00:00 in user’s preferred time zone.
Below we set the long value for minimal date supported by the CalendarView.
calendarView1.setMinDate(1463918226920L);
 8. getMinDate():
This method is used to to get the minimal date supported by thisCalendarView in milliseconds since January 1, 1970 00:00:00 in user’s preferred time zone. This method returns long type value for minimal date supported by this CalendarView.
Below we firstly set the long value for the minimal date and then get the minimal value supported by the CalendarView.
long getmindate = calendarView1.getMinDate();
9. setShowWeekNumber(boolean showWeekNumber):
This method is used to show or hide the week number of CalendarView. In this method we set Boolean type value means true or false.
calendarView1.setShowWeekNumber(true);
 // set true value for showing the week numbers.

10. getShowWeekNumber():
This method is used to check whether the week number are shown or not. This method returns Boolean type value means true or false. True indicates week numbers are shown and false indicates week numbers are currently hidden.
Below we checks whether the week number are currently showing or not.
Boolean showWeekNo = calendarView1.getShowWeekNumber();

if(showWeekNo == true)

{

    Toast.makeText(this,"Week Number showing",Toast.LENGTH_LONG).show();

}else {

    Toast.makeText(this,"Week Number not showing",Toast.LENGTH_LONG).show();

}
 // checks whether the week number are shown or not.

24. setOnDateChangeListener(OnDateChangeListenerlistener)
This method is used to set the listener to be notified upon selected date change.
Below we show how to use setOnDateChangeListener event of CalendarView.
calendarView1=(CalendarView)findViewById(R.id.calender1);



calendarView1.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

    @Override

    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

        long lDate=calendarView1.getDate();

        String sDate=String.valueOf(lDate);

        Toast.makeText(getApplicationContext(),sDate,Toast.LENGTH_SHORT).show();

       // textView.setText(sDate);

    }

});





















































22

No comments:

Post a Comment