Calendar in Android app to select month and year - android

I want to have a calendar that would allow the user to only choose month and year. Are there any 3rd party widgets that would allow me to do that? I know about date slider (http://i.stack.imgur.com/BDFls.png) that allows me to create a dialog box where user would choose month and year, but is there a widget that is similar to what I posted below? Or would I have to create this myself?

I would recommend CalDroid. It has customization options to allow you to create the desired effect.
Edit: Read the documentation on the project's GitHub:
Caldroid fragment includes 4 main parts:
1) Month title view: show the month and year (e.g MARCH, 2013)
2) Navigation arrows: to navigate to next month or previous month
3) Weekday gridview: contains only 1 row and 7 columns. To display "SUN, MON, TUE, WED, THU, FRI, SAT"
4) An infinite view pager that allow user to swipe left/right to change month. This library is taken from https://github.com/antonyt/InfiniteViewPager
Number (1) seems to be what you need.

Personally, for this reasonably simple use case, I'd not bother with some 3rd-party thing you can't easily debug/maintain. I'd use two ViewFlippers -- one for month and one for day. ViewFlipper supports animation, so you can get some nice looking physics to it when the user spins to a month/day.

yes, there is, Calendar Times Square provides to you a calendar to display and then a way to choose from it some date:
first, import the calendar into your project.
then, display it the following way:
//get the current date:
Calendar firstYear = Calendar.getInstance();
Calendar lastYear = Calendar.getInstance();
firstYear.add(Calendar.YEAR, -10);
lastYear.add(Calendar.YEAR, 10);
// to declare the calendar with the current date and make it scrollable for a past year and an upcoming year
final CalendarPickerView calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
calendar.init(firstYear.getTime() , lastYear.getTime())
.withSelectedDate(Calendar.getInstance().getTime());
then, when want to save the selected date:
Date new_date = calendar.getSelectedDate();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new_date);
// to make sure it saved to the system
if (ShellInterface.isSuAvailable()) {
ShellInterface.runCommand("chmod 666 /dev/alarm");
SystemClock.setCurrentTimeMillis(calendar.getTimeInMillis());
ShellInterface.runCommand("chmod 664 /dev/alarm");
}

Related

How to show Material Calendar with all disabled dates in Android datePickerDialog

I am trying to make a calendar date picker dialog pop-up with a condition. User selects month and year at first. Hence, DatePickerDialog pop-up will show with minimum 28 days (for February only) and maximum 31days (for January, March, May, …, December). User is able to pick particular date in a previously selected month of the year. Thus, those dates on which user gave attendance are set enabled, rest dates are set disabled. Now, there may be a situation that user was absent for the whole month. I want to show all the dates set disabled in the calendar for that particular month of the year. I was looking at the documentation that there were methods called setSelectableDays(Calendar[] days) and setDisabledDays(Calendar[] days), both takes #NonNull array of calendar objects to enable and disable dates repectively.
So, for this case if there was no attendance date for a particular month, I tried to make a absentDates array of calendar and passed it to the second method i.e. setDisabledDays(Calendar[] days). But, it not showing anything. I tried and checked that except one day in a specific month, I can disable rest days, not all the dates at once (an image is attached of that). I want all the dates disabled in the DatePickerDialog pop-up.
If you use the official MaterialDatePicker you can implement your own DateValidator to enable/disable days and setting it to the CalendarConstraints object before building the dialog with MaterialDatePicker.Builder().datePicker().setCalendarConstraints(contraints)
You can find a sample implementation here for DateValidatorPointForward.
Documentation: https://material.io/components/date-pickers/android#using-date-pickers

Android CalendarView how to detect Month change

I need to search my database basing the query on the month returned from a CalendarView. My problem is that onSelectedDayChange() event fires only when clicking, actually selecting a date not when the month is changed by swiping CalendarView.
How to set up something like "onSelectedMonthChange"?
You can't do it with the Android CalendarView. Look at this thread.
If you want to do it, you will have to make custom CalendarView class which will extend CalendarView and implement onGestureListener. Keep a variable curMonth to keep account of your month. Whenever swipe left/right happen, update your month variable accordingly.
if you want to show next and previous months in your calendar based on some click event, then you can do it as:
Calendar c = Calendar.getInstance();
//for next month
btnNext.onClick(){
c.add(Calendar.MONTH,1);
}
//previous month
btnPrevious.onClick(){
c.add(Calendar.MONTH,-1);
}

How do I set the date for a date picker?

I use DatePickerDialog.OnDateSetListener that's works fine.
I want to add date for 120 days in date picker.
What I mean is if I add 120 days, the date and month will be change automatically.
How to do it?
Something like this should do the trick:
Calendar cal = Calendar.getInstance();
cal.set(datepick.getYear(), datepick.getMonth() + 1, datepick.getDayOfMonth());
cal.add(Calendar.DATE, 120);
datepick.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, cal.get(Calendar.DATE));
Make sure you create a date object with 120 days added (see this topic on how to do that) and use that to populate your datepicker, either on initialization or when changed. I'm not really sure what you are trying to achieve however the latter doesn't seem right usability wise. In that case I would create an extra textfield that represents the +120 days date.
Set Date Programmatically by using UpdateDate
datePickerDialog.UpdateDate(selectedDate ?? DateTime.Now);

how to get next day of current date in a MonthDisplayHelper in android?

i have a monthdisplayhelper to display the month, i want to select days between two date. i.e.,if i selected 2nd July and 9th July, all the dates(3rd to 8th) in between these dates must be selected or highlighted.
You can use the Calendar class. Initialise a new Calendar object for your initial date, then use [calendar instance].add(Calendar.DAY_OF_YEAR, 1) to increment the date by 1 day, it'll automatically roll to the next month if required.
Calendar reference: http://developer.android.com/reference/java/util/Calendar.html
If you know how many days you will need then you could have a loop basically add days and store in a list

How to compare date and year?

i am implementing one graph related app.
In that when application start i am getting present month,present year from calende and display on the screen with graph. like April 2012 and graph
ok that is fine.
In my screen i have two buttons like preview,next.
when user clicks preview ,i want to change April 2012 to March 2012,Fib 2012 ..........
And when user clicks next button,i want to change April 2012 to May 2012.....
How i will do these type of comparigens.
If any one know please help me.
Thanks in advance
Use Calendar Instance to set time.. When user presses next increment month by one and set it to Calendar and get Date from it.. and The opposite for previous.. But using default DatePicker is a lot better Option..
For this purpose you can use the add(int, int) method on a Calendar instance.
mCalendar.add(Calendar.Month, 1) // next month
mCalendar.add(Calendar.Month, -1) // previous month
Calendar mCalendar = Calendar.getInstance();
int cmonth = mCalendar.get(Calendar.MONTH);
int cyear = mCalendar.get(Calendar.YEAR);
//when you click next in click event call this below three lines
mCalendar.add(Calendar.MONTH, 1); // next month
cmonth = mCalendar.get(Calendar.MONTH);
cyear = mCalendar.get(Calendar.YEAR);
//when you click previous in click event call this below tthree lines
mCalendar.add(Calendar.MONTH, -1); // previous month
cmonth = mCalendar.get(Calendar.MONTH);
cyear = mCalendar.get(Calendar.YEAR);

Categories

Resources