I want to snow the traditional(Before material design) calendar in my application : counter calendar
I use the following code and this is the output
enter image description here
my code is :
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Log.e("Result is :", String.valueOf(year + month + day));
}
i need to show this calendar .. how i can ?
enter image description here
Related
I am storing date and time but I need to have it limited to only be allowed to pick intervals of 30 minutes when the time picker is displayed so I can't be able for example to pick time 06:11 .
Here is my code that I have.
private void showDateTimeDialog(final EditText date_time_in) {
final Calendar calendar=Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener=new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
TimePickerDialog.OnTimeSetListener timeSetListener=new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
//format of the calender
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy-MM-dd HH:mm");
//add the booking in Firebase
date_time_in.setText(simpleDateFormat.format(calendar.getTime()));
Bookings booking = new Bookings(year,month+1,dayOfMonth,hourOfDay,minute);
rootDatabaseRef.push().setValue(booking);
Toast.makeText(BookingActivity.this, "Appointment successful ", Toast.LENGTH_SHORT).show();
date_time_in.setText(getString(R.string.select_a_date_and_timeText));
}
};
new TimePickerDialog(BookingActivity.this,timeSetListener,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),false).show();
}
};
new DatePickerDialog(BookingActivity.this,dateSetListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
What changes do I need to make to limit to 30 minute intervals ?
I want to set restrictions on the Datepicker so that it will allow the user to only choose the date 7 days from now. I also want to set restrictions on the Timepicker so that the user can only choose the time between 11:00 am and 6:00 pm. Here is my Code for the DatePicker and TimePicker.
private void showTimeDialog(final EditText timeInput) {
final Calendar calendar = Calendar.getInstance();
TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
timeInput.setText(enteredTime.format(calendar.getTime()));
time = enteredTime.format(calendar.getTime());
}
};
new TimePickerDialog(OrderPickupActivity.this, timeSetListener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false).show();
}
private void showDateDialog(final EditText dateInput) {
final Calendar calendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
dateInput.setText(enteredDate.format(calendar.getTime()));
date = enteredDate.format(calendar.getTime());
}
};
new DatePickerDialog(OrderPickupActivity.this, dateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
Can you please help me out?
private void showDateDialog(final EditText dateInput) {
final Calendar calendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
dateInput.setText(enteredDate.format(calendar.getTime()));
date = enteredDate.format(calendar.getTime());
}
};
DatePickerDialog datePickerDialog = new
DatePickerDialog(OrderPickupActivity.this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 7); // Adding 7 days
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
datePickerDialog.show();
}
I am trying to set a datepicker by timezone specified by the app. I have succeed somewhat in applying the current selection and minDates but I can't get today date (bold number on date picker dialogue) to change. I have uploaded a picture which shows datepicker using getDefult timezone to set today date instead of specified timezone.
How can I specify today date?
billDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
billDateDialog.show();
billDateDialog.updateDate(billDateCal.get(Calendar.YEAR), billDateCal.get(Calendar.MONTH), billDateCal.get(Calendar.DAY_OF_MONTH));
}
});
billDateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
long correctedTime = new DayLightSavings(BillsAdd.this, year, monthOfYear, dayOfMonth, timeZone).getCorrectedTime();
billDateCal.setTimeInMillis(correctedTime);
billDate.setText(dateFormatter.format(billDateCal.getTimeInMillis()));
}
},billDateCal.get(Calendar.YEAR), billDateCal.get(Calendar.MONTH), billDateCal.get(Calendar.DAY_OF_MONTH));
long oneDay = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
long oldRawOffset = TimeZone.getDefault().getRawOffset();
long newRawOffset = timeZone.getRawOffset();
long rawOffset = oldRawOffset - newRawOffset;
billDateDialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis() + oneDay - rawOffset);
You can check this code.This might be helpful for you...
//Create calendar instance
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
final DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
tvStartdate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
tvStartdate.setTextColor(Color.BLACK);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = sdf.format(new Date(year - 1900, monthOfYear, dayOfMonth));
sd = formatedDate;
}
}, year, month, day);
dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
dpd.show();
I am not able to understand where am I doing mistake. I want to check it is a weekend or weekday. If it is weekend then it should prompt in the toast that it is a sunday or saturday weekend. I don't know what should I put in place of ??? mark. It should also display current date if it is weekend. Please, any one can help me. Thank you.
View.OnClickListener//,
// TimePickerDialog.OnTimeSetListener
// TimePickerDialog.OnTimeSetListener
{
//Declaration for class
ButtonViews views;
dpListener dpListenerView;
// Declartion for member vairables
int day, month, x_year;
int hour;
int minute;
Calendar calendar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
views = new ButtonViews();
dpListenerView = new dpListener();
//ButtonListener
views.button_date.setOnClickListener(this);
views.button_time.setOnClickListener(this);
//
// pick up the default date using Calender class
calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
/*
day = calendar.get(Calendar.DAY_OF_MONTH);
month = calendar.get(Calendar.MONTH);
x_year = calendar.get(Calendar.YEAR);
*/
curr_date();
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
setupDate(day, month, x_year);
setupTime(hour, minute);
}
public void curr_date(){
day = calendar.get(Calendar.DAY_OF_MONTH);
month = calendar.get(Calendar.MONTH);
x_year = calendar.get(Calendar.YEAR);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_date:
showDatePickerDialog();
break;
case R.id.button_time:
break;
}
}
private void setupTime(int hours, int minutes) {
views.button_time.setText(hours + ":" + minutes);
}
private void setupDate(int day, int month, int year) {
String strMonth = ((month + 1) <= 9) ? ("0" + (month + 1)) : String.valueOf(month + 1);
views.button_date.setText(String.valueOf(day) + "/" + strMonth + "/" + String.valueOf(year));
}
private void showDatePickerDialog() {
DatePickerDialog datepickerdialog = new DatePickerDialog
(
this,
dpListenerView,
/* new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
} },*/
//this,
x_year,
month,
day
);
datepickerdialog.show();
}
class ButtonViews {
Button button_time;
Button button_date;
public ButtonViews() {
button_date = (Button) findViewById(R.id.button_date);
button_time = (Button) findViewById(R.id.button_time);
}
}
class dpListener implements OnDateSetListener {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar c = Calendar.getInstance();
// c.setTime();
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek == 7 || dayOfWeek == 1) {
// as your requirement: you should display mesage they can not select the weekend" here
// then you set the value in datepickerdialog by current date
Toast.makeText(DateTimePickerActivity.this
, "You have selected weekend"
, Toast.LENGTH_SHORT).show();
}
}
}
}
Once you get your date you can do like this and get the day and proceed with your logic.
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
I think that your variable dayOfWeek is the same variable as the parameter dayOfMonth
The above code must work
public DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
if (selectedDay == 1 || selectedDay == 7) {
Toast.makeText(DateTimePickerActivity.this
, "You have selected weekend"
, Toast.LENGTH_SHORT).show();
view.updateDate(selectedYear,selectedMonth,selectedDay);
}
}
};
in my app i am trying to show a date picker in OnTouchEvent. When the date picker dialog appears it shows the Month in text such as May, June, July etc. When i select one among them it displays in numbers. I want it to be viewed as text itself. How to get it.....
Following is the part of my code
bd =(EditText)findViewById(R.id.dob);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
private OnTouchListener bdListener = new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
showDialog(DATE_DIALOG_ID);
return false;
}
};
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
{
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private void updateDisplay()
{
bd.setText(new StringBuilder().append(mMonth+1).append("-").append(mDay).append("-").append(mYear));
}
So you have the month as an integer and you want the string representation.
I think you need DateFormatSymbols
monthText = new DateFormatSymbols().getMonths()[month];