banner



How To Set Calendar Date In Android

Calendar View Tutorial With Example In Android Studio

In Android, Calendar View widget was added in API level 11(Android version iii.0) which ways this view is only supported in the device that are running on Android three.0 and higher version. It is used for displaying and selecting dates.

Calendar View in Android

The supported range of dates of this agenda is configurable. User tin select a date by taping/clicking on it and as well can roll & observe the calendar to a desired date. Developer can also set up minimum and maximum date shown in calendar view.


Bones Calendar View XML Code:

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" />        

Calendar View


Important Methods Of Calendar View:

Permit's nosotros discuss some of import methods of Calendar View that may be chosen in social club to manage the CalendarView.

1. getDate(): This method is used to become the selected engagement 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.

Below we get the selected of CalendarView in milliseconds.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView long selectedDate = simpleCalendarView.getDate(); // go selected date in milliseconds        

two. setDate(long appointment): This method is used to gear up the selected date in milliseconds since Jan 1, 1970 00:00:00 in user's preferred  fourth dimension zone.

Below nosotros set the selected engagement in milliseconds for a CalendarView.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // become the reference of CalendarView simpleCalendarView.setDate(1463918226920L); // set selected date 22 May 2022 in milliseconds        

setDate in Calendar View Android

three. setFirstDayOfWeek(int firstDayOfWeek): This method is used to set the first day of the calendar week.

Beneath we set the two value means Monday as the get-go day of the week.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView simpleCalendarView.setFirstDayOfWeek(ii); // set Monday as the first day of the week        

setFirstDayOfWeek in CalendarView Android

iv. getFirstDayOfWeek(): This method is used to get the first day of week. This method returns an int type value.

Below we get the first twenty-four hours of the week of CalendarView.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView int firstDayOfWeek= simpleCalendarView.getFirstDayOfWeek(); // get first twenty-four hour period of the week        

5. setMaxDate(long maxDate):  This method is used to gear up the maximal appointment supported past thisCalendarView in milliseconds since January 1, 1970 00:00:00 in user'due south preferred time zone.

Below we gear up the long value for maximal date supported by the CalendarView.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // become the reference of CalendarView simpleCalendarView.setMaxDate(1463918226920L); // set 22nd May 2022 as max date supported by this CalendarView        

setMaxDate in CalendarView Android

six. getMaxDate(): This method is used to become the maximal engagement supported past this CalendarView in milliseconds since January 1, 1970 00:00:00 in user'south preferred fourth dimension zone. This method returns long blazon value for maximal appointment supported by this CalendarView.

Below we firstly set the long value for the maximal appointment and then get the maximal value supported by the CalendarView.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView simpleCalendarView.setMaxDate(1463918226920L); // set up max engagement supported by this CalendarViewlong maxDate= simpleCalendarView.getMaxDate(); // become max date supported by this CalendarView        

seven. 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'due south preferred time zone.

Below we gear up the long value for minimal date supported past the CalendarView.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView simpleCalendarView.setMinDate(1463918226920L); // fix min engagement supported by this CalendarView        

8. getMinDate(): This method is used to to get the minimal date supported by thisCalendarView in milliseconds since Jan i, 1970 00:00:00 in user'south 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 so become the minimal value supported by the CalendarView.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // go the reference of CalendarView simpleCalendarView.setMinDate(1463918226920L); // gear up min date supported past this CalendarViewlong minDate= simpleCalendarView.getMinDate(); // go min date supported past this CalendarView        

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 ways true or faux.

Below we set the true value for showing the week numbers of CalendarView.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView simpleCalendarView.setShowWeekNumber(truthful); // set truthful value for showing the calendar week numbers.        

setShowWeekNumber in Calendar View Android

10. getShowWeekNumber(): This method is used to bank check whether the week number are shown or not. This method returns Boolean type value means true or false. True indicates calendar week numbers are shown and false indicates week numbers are currently subconscious.

Below we checks whether the week number are currently showing or non.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // become the reference of CalendarView simpleCalendarView.getShowWeekNumber(); // checks whether the week number are shown or not.        

11. getSelectedDateVerticalBar(): This method is used to get the drawable i.e used for the vertical bar shown at the beginning and at the finish of the selected date. This method was deprecated in API level 23 so no longer used by Material style CalendarView.

Beneath nosotros become the practical drawable for the vertical bar shown at the kickoff and at the finish of the selected date.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // go the reference of CalendarView Drawable verticalBar=simpleCalendarView.getSelectedDateVerticalBar(); // go the applied drawable for the vertical bar        

12. setSelectedDateVerticalBar(Drawabledrawable): This method is used to set the drawable for the vertical bar shown at the beginning and at the end of the selected date. This method was deprecated in API level 23 then no longer used by Material fashion CalendarView.

Below nosotros set the drawable for the vertical bar shown at the get-go and at the end of the selected appointment.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // become the reference of CalendarView simpleCalendarView.setSelectedDateVerticalBar(getResources().getDrawable(R.drawable.ic_launcher)); // set the drawable for the vertical bar        

setSelectedDateVerticalBar in Calendar View Android

13. setSelectedDateVerticalBar(int resourceId): This method is used to prepare the color for the vertical bar shown at the commencement and at the end of the selected date. This method was deprecated in API level 23 so no longer used by Material fashion CalendarView.

Below we set the blue color for the vertical bar shown at the beginning and at the cease of the selected appointment.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // go the reference of CalendarView simpleCalendarView.setSelectedDateVerticalBar(Color.BLUE); // prepare the colour for the vertical bar        

14. setSelectedWeekBackgroundColor(int colour): This method is used to ready the color in the background of selected week of CalendarView. This method was deprecated in API level 23 so no longer used by Material mode CalendarView.

Below we set the blackness color in the background of selected week of CalendarView.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView simpleCalendarView.setSelectedWeekBackgroundColor(Colour.Black); // prepare black color in the groundwork of selected week

setSelectedWeekBackgroundColor in Calendar View Android

15. getSelectedWeekBackgroundColor(): This method is used to become the background color of selected week of CalendarView. This method returns an int type value. This method was deprecated in API level 23 and so no longer used by Material style CalendarView.

Below we get the applied color in the background of selected week of CalendarView.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // become the reference of CalendarView int backColor=simpleCalendarView.getSelectedWeekBackgroundColor(); // get applied color in the background of selected calendar week        

16. getWeekNumberColor(): This method is used to go the color of week numbers. This method returns an int type value. This method was deprecated in API level 23 and then no longer used by Material style CalendarView.

Beneath we go the applied colour of week numbers.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView int weekNumberColor=simpleCalendarView.getWeekNumberColor(); // get applied colour of calendar week number        

17. setWeekNumberColor(int color): This method is used to set the color for the week numbers. This method was deprecated in API level 23 so no longer used past Material way CalendarView.

Beneath we prepare the ruddy color of calendar week numbers.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // become the reference of CalendarView simpleCalendarView.setWeekNumberColor(Color.RED); // ready cherry-red color for the calendar week number        

18. setWeekSeparatorLineColor(int color): This method is used to set the colour for the week separator line. This method was deprecated in API level 23 and so no longer used by Material style CalendarView.

Below we set the green color for the week separator line.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView simpleCalendarView.setWeekSeparatorLineColor(Color.GREEN); // set green color for the week separator line.        

19. getWeekSeparatorLineColor(): This method is used to get the colour of week separator line. This method returns an int blazon value. This method was deprecated in API level 23 so no longer used by Material style Calendar View.

Below we get the practical color of week separator line.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // become the reference of CalendarView int weekSeperatorLineColor=simpleCalendarView.getWeekSeparatorLineColor(); // get practical color of week seperator line        

twenty. setUnfocusedMonthDateColor(int color): This method is used to gear up the color for the dates of an unfocused month. This method was deprecated in API level 23 so no longer used past Textile manner Calendar View.

Below we gear up the gray color for the dates of an unfocused month.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView simpleCalendarView.setUnfocusedMonthDateColor(Colour.Grey); // set gray color for the dates of an unfocused calendar month        

21. getUnfocusedMonthDateColor(): This method is used to get the color for the dates of an unfocused month. This method returns int type value. This method was deprecated in API level 23 so no longer used by Cloth style CalendarView.

Below we get the practical colour for the dates of an unfocused calendar month.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView int unfocusedMonthDateColor=simpleCalendarView.getUnfocusedMonthDateColor(); // become the color for the dates of an unfocused month        

22. setFocusedMonthDateColor(int colour): This method is used to set the color for the dates of an focused month. This method was deprecated in API level 23 so no longer used by Cloth style CalendarView.

Below nosotros fix the xanthous colour for the dates of an focused month.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView simpleCalendarView.setFocusedMonthDateColor(Color.Yellow); // set yellow color for the dates of focused month        

23. getFocusedMonthDateColor(): This method is used to get the colour for the dates of an focused month. This method returns int type value. This method was deprecated in API level 23 so no longer used by Material style CalendarView.

Below nosotros get the applied colour for the dates of an focused calendar month.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView int focusedMonthDateColor=simpleCalendarView.getFocusedMonthDateColor(); // become the color for the dates of focused month        

24. setOnDateChangeListener(OnDateChangeListenerlistener): This method is used to fix the listener to be notified upon selected appointment modify.

Beneath nosotros evidence how to use setOnDateChangeListener upshot of CalendarView.

CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView // perform setOnDateChangeListener upshot on CalendarView simpleCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { // add lawmaking here } });        

Attributes of CalendarView:

Now allow's we discuss some mutual attributes of a CalendarView that helps u.s.a. to configure it in our layout (xml).

1. id: id attribute is used to uniquely identify a CalendarView.

Beneath we set the id of the CalendarView that is used to uniquely place it.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <!--  id of the CalendarView that is used to uniquely identify information technology -->        

2. firstDayOfWeek: This attributes is used to set the first mean solar day of calendar week co-ordinate to Calendar. We can also fix this programmatically ways in java course using setFirstDayOfWeek(int firstDayOfWeek) method

Below we set the two value ways Mon as the first twenty-four hours of the week.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:firstDayOfWeek="2" /> <!-- set the two value means Monday equally the first day of the week -->        

3. focusedMonthDateColor: This attribute is used to set the color for the dates of the focused month. We can also set up this programmatically means in java grade using setFocusedMonthDateColor(int color) method.

Below we set the yellow color for the dates of an focused month.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:firstDayOfWeek="2" android:focusedMonthDateColor="#ff0" /> <!-- set the xanthous colour for the dates of focused month -->        

four. unfocusedMonthDateColor: This attribute is used to set the color for the dates of the unfocused calendar month. Nosotros tin can besides set this programmatically ways in java form using setUnfocusedMonthDateColor(int colour) method.

Below nosotros set the Red colour for the dates of an focused month.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:firstDayOfWeek="ii" android:unfocusedMonthDateColor="#f00" /> <!-- set the yellow colour for the dates of an unfocused month -->        

five. maxDate: This attribute is used to gear up the maximal engagement supported by this CalendarView. This attribute employ mm/dd/yyyy format. We tin too fix this programmatically means in java grade using setMaxDate(long maxDate)

Below we set the 05/22/2017 as maximal appointment supported by the CalendarView.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:firstDayOfWeek="2" android:maxDate="05/22/2017" /> <!-- set maximal date supported by this CalendarView -->        

6. minDate: This attribute is used to set the minimal appointment supported by this CalendarView. This attribute employ mm/dd/yyyy format. We can also set this programmatically ways in java class using setMinDate(long minDate)

Below we set the 05/22/2016 equally minimal date supported past the CalendarView.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:firstDayOfWeek="2" android:minDate="05/22/2016" /> <!-- set minimal date supported by this CalendarView -->        

7. selectedDateVerticalBar: This attribute is used to set the drawable/colour for the vertical bar shown at the starting time and at the end of the selected appointment. Nosotros can as well set up this programmatically means in java class using setSelectedDateVerticalBar(int resourceId) or setSelectedDateVerticalBar(Drawable drawable) method.

Below we set the black colour for the vertical bar shown at the commencement and at the stop of the selected date.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:firstDayOfWeek="2" android:selectedDateVerticalBar="@color/black" /> <!-- gear up black color for the vertical bar shown at the beginning and at the finish of the selected date -->        

8. selectedWeekBackgroundColor: This aspect is used to set the color in the groundwork of selected week of CalendarView. Nosotros can as well gear up this programmatically in java class using setSelectedWeekBackgroundColor(int colour) method.

Below we set the ruby-red colour in the background of selected calendar week of CalendarView.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:firstDayOfWeek="2" android:selectedWeekBackgroundColor="#f00" /> <!-- set crimson in the groundwork of selected week of CalendarView -->        

9. showWeekNumber: This attribute is used to show or hibernate the week number of CalendarView. In this method nosotros set Boolean type value means true or false. Nosotros can also set this programmatically means in coffee class using setShowWeekNumber(boolean showWeekNumber) method.

Below we fix the imitation value for hiding the week numbers of CalendarView.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:firstDayOfWeek="two" android:showWeekNumber="false" /> <!-- set the imitation value for hiding the calendar week numbers of CalendarView. -->        

10. weekNumberColor: This attribute is used to set the color for the week numbers.

Below we set the red color of calendar week numbers. We tin can also set this programmatically ways in java course using setWeekNumberColor(int color) method.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:firstDayOfWeek="ii" android:weekNumberColor="#f00" /> <!-- set red color for the calendar week numbers of CalendarView. -->        

11. weekSeparatorLineColor: This attribute is used to set the color for the week separator line. Nosotros can also ready this programmatically ways in java class using setWeekSeparatorLineColor(int color) method.

Below we set the green color for the week separator line.

<CalendarView android:id="@+id/simpleCalendarView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:firstDayOfWeek="2" android:weekSeparatorLineColor="#0f0" /> <!-- set green colour for the week seperator line -->        

Calendar View Example In Android Studio:

Beneath is the example of CalendarView in which we brandish a CalendarView for a minimal and maximal supported engagement. In this we set 01/01/2016 minimal and 01/01/2018 as maximal engagement for this calendarView. We set focused and unfocused month engagement colour and also used some other functions and aspect for more customization of CalendarView. Finally we perform setOnDateChangeListener issue to be notified upon selected date alter. Whenever a user tap/click on any date the selected date will be displayed by using a Toast.

Below you can download the Android Studio project code, encounter final output and step past step explanation:

Download Code ?

Calendar View Example In Android Studio

Stride one: Create a new project and proper noun information technology CalendarViewExample

Step two: Open res -> layout ->activity_main.xml (or) main.xml and add together following code:

<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"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context=".MainActivity">      <!-- CalendarView with monday every bit first day and minmal and maximal twenty-four hour period -->     <CalendarView         android:id="@+id/simpleCalendarView"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:firstDayOfWeek="two"         android:maxDate="01/01/2018"         android:minDate="01/01/2016" />   </RelativeLayout>

Stride iii:  Open   src -> package -> MainActivity.java

In this stride we open MainActivity and add the lawmaking to initiate the Agenda View. In this nosotros gear up selected week groundwork color and week separator line color and finally perform setOnDateChangeListener event to be notified upon selected date change. Whenever a user tap/click on whatever appointment the selected appointment will exist displayed by using a Toast.

parcel case.abhiandroid.calenderviewexample;  import android.graphics.Color; import android.graphics.drawable.Drawable; import android.support.v7.app.AppCompatActivity; import android.os.Parcel; import android.view.Card; import android.view.MenuItem; import android.widget.CalendarView; import android.widget.Toast;  public class MainActivity extends AppCompatActivity {      CalendarView simpleCalendarView;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         simpleCalendarView = (CalendarView) findViewById(R.id.simpleCalendarView); // get the reference of CalendarView         simpleCalendarView.setFocusedMonthDateColor(Colour.RED); // set the red color for the dates of  focused month         simpleCalendarView.setUnfocusedMonthDateColor(Colour.BLUE); // set up the yellowish color for the dates of an unfocused calendar month         simpleCalendarView.setSelectedWeekBackgroundColor(Color.RED); // red colour for the selected week's background         simpleCalendarView.setWeekSeparatorLineColor(Color.GREEN); // green color for the week separator line         // perform setOnDateChangeListener event on CalendarView         simpleCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {             @Override             public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {                 // display the selected date by using a toast                 Toast.makeText(getApplicationContext(), dayOfMonth + "/" + month + "/" + yr, Toast.LENGTH_LONG).show();             }         });     }   }

Output:

Now run the app and you will run across Calendar open. Now click on whatsoever date and it volition displayed on the screen as Toast. Too attempt scrolling up and downwardly to see the maximum and minimum date gear up for Agenda.

How To Set Calendar Date In Android,

Source: https://abhiandroid.com/ui/calendarview

Posted by: brubakeralks1993.blogspot.com

0 Response to "How To Set Calendar Date In Android"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel