Limit Time Range in Time Picker - java

How to control TimePicker, I just want to show time range in time picker from 08:00 am to 02:00 pm (except from 08:00 am to 02:00 pm hide all)
I have written complete code to show TimePicker, and customized time as well.
Here is my code:
case DIALOG_TIME:
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(this, lisTime, hour, minute,
DateFormat.is24HourFormat(MainActivity.this));
TimePickerDialog.OnTimeSetListener lisTime = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
String meridiem = "";
nim = minute;
Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
datetime.set(Calendar.MINUTE, minute);
if (datetime.get(Calendar.AM_PM) == Calendar.AM)
meridiem = "AM";
else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
meridiem = "PM";
hour = (datetime.get(Calendar.HOUR) == 0) ?"12":String.valueOf(datetime.get(Calendar.HOUR));
String time = pad(Integer.parseInt(hour)) + ":" + pad(minute) + " " + meridiem;
editTime.setText(time);
}
};
private String pad(int value){
if(value<10){
return "0"+value;
}
return ""+value;
}

As in this answer: https://stackoverflow.com/a/20396673/4130107
You can create your custom TimePickerDialog and overwrite the onAttachedToWindow(); but instead limit the hour range:
private final boolean mIs24HourView;
public CustomTimePickerDialog(Context context, OnTimeSetListener callBack,
int hourOfDay, int minute, boolean is24HourView) {
super(context, callBack, hourOfDay, minute, is24HourView);
mIs24HourView = is24HourView;
}
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
try {
Class<?> classForid = Class.forName("com.android.internal.R$id");
Field timePickerField = classForid.getField("timePicker");
this.timePicker = (TimePicker) findViewById(timePickerField
.getInt(null));
Field field = classForid.getField("hour");
final NumberPicker mHourSpinner = (NumberPicker) timePicker
.findViewById(field.getInt(null));
if (mIs24HourView) {
mHourSpinner.setMinValue(2);
mHourSpinner.setMaxValue(20);
}else {
Field amPm = classForid.getField("amPm");
mHourSpinner.setMinValue(2);
final NumberPicker amPm = (NumberPicker) timePicker
.findViewById(amPm.getInt(null));
amPm.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker np1, int oldVal, int newVal) {
if (newVal == 0) { // case AM
mHourSpinner.setMinValue(2);
mHourSpinner.setMaxValue(12);
} else { // case PM
mHourSpinner.setMinValue(1);
mHourSpinner.setMaxValue(8);
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
EDIT: now working for AM/PM too

Related

Between two times in Android Studio

Trying to get between two times in Android Studio. In specific, 5PM and 11PM. I can get it to get in between those times, but when I select a time with a minute that isn't 11:00PM or 5:00PM it becomes true. Is there an easier way to do this?
Here's my code so far
Calendar c = Calendar.getInstance();
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
if(hourOfDay >= 23 && hourOfDay <= 17){
reservation.setText("The time that your food will arrive will be " + timeFormat.format(c.getTime()) + ".");
}else{
reservation.setText("Please select a time between 5PM and 11PM");
}
//reservation.setText(hourOfDay + " " + minute);
}
check for minutes when hour is 23.
if((hourOfDay >= 17 && hourOfDay <= 22)||(hourOfDay==23 && minute==0)) {
//within 15:00 to 23:00
}else {
//outside of time
}
if(hourOfDay >= 23 && hourOfDay <= 17){
How can one hourOfDay be both <= 17 and >= 23 ? try switching && to ||
You can use the below widget to show TimeDialog with range settings.
package com.mysuger.android.dialogs;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.util.Calendar;
import android.app.TimePickerDialog;
import android.content.Context;
import android.widget.TimePicker;
/**
* A time dialog that allows setting a min and max time.
*
*/
public class RangeTimePickerDialog extends TimePickerDialog {
private int minHour = -1;
private int minMinute = -1;
private int maxHour = 25;
private int maxMinute = 25;
private int currentHour = 0;
private int currentMinute = 0;
private Calendar calendar = Calendar.getInstance();
private DateFormat dateFormat;
public RangeTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) {
super(context, callBack, hourOfDay, minute, is24HourView);
currentHour = hourOfDay;
currentMinute = minute;
dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
try {
Class<?> superclass = getClass().getSuperclass();
Field mTimePickerField = superclass.getDeclaredField("mTimePicker");
mTimePickerField.setAccessible(true);
TimePicker mTimePicker = (TimePicker) mTimePickerField.get(this);
mTimePicker.setOnTimeChangedListener(this);
} catch (NoSuchFieldException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}
public void setMin(int hour, int minute) {
minHour = hour;
minMinute = minute;
}
public void setMax(int hour, int minute) {
maxHour = hour;
maxMinute = minute;
}
#Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
boolean validTime = true;
if (hourOfDay < minHour || (hourOfDay == minHour && minute < minMinute)){
validTime = false;
}
if (hourOfDay > maxHour || (hourOfDay == maxHour && minute > maxMinute)){
validTime = false;
}
if (validTime) {
currentHour = hourOfDay;
currentMinute = minute;
}
updateTime(currentHour, currentMinute);
updateDialogTitle(view, currentHour, currentMinute);
}
private void updateDialogTitle(TimePicker timePicker, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
String title = dateFormat.format(calendar.getTime());
setTitle(title);
}
}
Call it from the below mentioned way,
Calendar c = Calendar.getInstance();
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
reservation.setText("The time that your food will arrive will be " + timeFormat.format(c.getTime()) + ".");
}
RangeTimePickerDialog d = new RangeTimePickerDialog(Activity.this, t, 17, 0, true);
d.setMin(17, 0);
d.setMax(23, 0);
d.show();
For more details, please refer to this

Not display current time for android timePicker dialog

I have a timeIn editText in my app. When it is clicked, it will pop up TimePicker dialog.
timeIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { //shows timePick
isEdited=true;
Fragment fragDialog = getSupportFragmentManager().findFragmentByTag("TimePicker");
if (fragDialog == null) { // Fragment not added
tp.setFlag(TimePick.FLAG_START_DATE);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
} else {
// already active
}
}
});
public static class TimePick extends android.support.v4.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {
public static final int FLAG_START_DATE = 00;
public static final int FLAG_END_DATE = 01;
private int flag = 00;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
public void setFlag(int i) {
flag = i;
}
#Override
public void onTimeSet(TimePicker view, int hourofDay, int minute) {
if (flag == FLAG_START_DATE) {
timeIn.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
TimeIn = timeIn.getText().toString();
}
if (flag == FLAG_END_DATE) {
timeOut.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
TimeOut = timeOut.getText().toString();
}
}
}
The time selected can be setText in timeIn. But when I click the timeIn editText again, the timePicker dialog popup with the current time . Is it possible to make it display the selected time instead of current time ? If it is first time click, then only show current time.
Isn't that what you are doing here
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
You are passing the current time to the dialog, that's why its showing the current time.
Solution:
Save the selected time and pass that to the class while creating the dialog and use that time in this code. If you don't have any selected time then use the current time. A little if-else block and some variables will do the trick.
Define these variable in outer class.
public static int inHour = -1;
public static int inMin = -1;
public static int outHour = -1;
public static int outMin = -1;
update onTimeSet method like this
if (flag == FLAG_START_DATE) {
inHour = hourofDay;
inMin = minute;
timeIn.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
TimeIn = timeIn.getText().toString();
}
else if (flag == FLAG_END_DATE) {
outHour = hourofDay;
outMin = minute;
timeOut.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
TimeOut = timeOut.getText().toString();
}
Replace your current code in onCreateDialog with
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int min = c.get(Calendar.MINUTE);
if((flag == FLAG_START_DATE) && inHour != -1 && inMin != -1)
{
hour = inHour;
min = inMin;
}
else if((flag == FLAG_END_DATE) && outHour != -1 && outMin != -1)
{
hour = outHour;
min = outMin;
}
return new TimePickerDialog(getActivity(), this, hour, min, DateFormat.is24HourFormat(getActivity()));

Datepicker dialog box does not open on click

When I run and click the button, it does not open the datapicker dialogbox. I am not able to find out what is incorrect. Please, any one can check and guide me where is the incorrect code in the program.
{
//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);
Log.i("DAY in Num....."," "+ month);
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
setupDate(day, month, x_year);
setupTime(hour, minute);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_date:
showDatePickerDialog();
break;
case R.id.button_time:
showTimePickerDialog();
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
);
}
/* private OnDateSetListener dpListener = new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
*//* day = dayOfMonth;
month = monthOfYear;
x_year = year;*//*
setupDate(dayOfMonth,monthOfYear,year);
}
};*/
public void showTimePickerDialog() {
TimePickerDialog timePickerDialog = new TimePickerDialog(
DateTimePickerActivity.this,
this,
hour,
minute,
true
);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
timePickerDialog.show();
}
// #Override
// public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// setupDate(dayOfMonth,monthOfYear,year);
// }
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setupTime(hourOfDay, minute);
}
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) {
/* day = dayOfMonth;
month = monthOfYear;
x_year = year;*/
setupDate(dayOfMonth,monthOfYear,year);
}
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_date:
showDialog(999);// Method for Show Dialog. You can use any Int
break;
case R.id.button_time:
showDialog(99);
break;
}
}
and Define this method for Identified dialog from int.
#Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 999) {
return new DatePickerDialog(this, myDateListener, yr, month, day);
} else if (id == 99) {
return new TimePickerDialog(this, myTimeListener, h, m, true);
}
return null;
}
And there Listeners
// For Date Picker
private DatePickerDialog.OnDateSetListener myDateListener = new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
day = dayOfMonth;
month = monthOfYear + 1;
yr = year;
Toast.makeText(
MainActivity.this,
new StringBuilder().append(day).append("-").append(month)
.append("-").append(yr), Toast.LENGTH_LONG).show();
}
};
// For TimePicker
private TimePickerDialog.OnTimeSetListener myTimeListener = new OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
h = hourOfDay;
m = minute;
Toast.makeText(MainActivity.this,
new StringBuilder().append(h).append(":").append(m),
Toast.LENGTH_LONG).show();
}
};

datepicker in android need to check weekend

I am doing datepicker. I am facing some challenges. I am checking wheather selected date is weekend or not. If it is weekend then it should give current date only. Now I don't know where should I put condition in this program and how to get the day of the week and against which thing I should check.
Please, help me.
// 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());
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:
/* showTimePickerDialog();*/
break;
}
}
private void setupTime(int hours, int minutes) {
views.button_time.setText(hours + ":" + minutes);
}
private void setupDate(int day, int month, int year) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
String strMonth = ((month + 1) <= 9) ? ("0" + (month + 1)) : String.valueOf(month + 1);
views.button_date.setText(String.valueOf(day) + "/" + strMonth + "/" + String.valueOf(year));
String strDate = String.valueOf(day) + "/" + strMonth + "/" + String.valueOf(year);
Date d = null;
try {
d = (Date) sdf.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
calendar.setTime(d);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek == 7 || dayOfWeek == 1) {
Toast.makeText(DateTimePickerActivity.this,"You have chosen weekend",Toast.LENGTH_SHORT);
}
}
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) {
setupDate(dayOfMonth, monthOfYear, year);
/* if(dayOfWeek == 7 || dayOfWeek == 1) {*/
// as your requirement: you should display message 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();
// }
}
}
}

Two date pickers connected to each other invoke two listeners at the same time

I have two date pickers which are connected to each other as when I change the date of the first picker the second ones date is also changed automatically ,
the two pickers have a listener called on date change listener, as follows
public class birthDate extends Activity{
Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH);
int currentDay = c.get(Calendar.DAY_OF_MONTH);
TextView birthDatetv;
DatePicker birthDayDatePicker,periodDatePicker;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.birthdate);
birthDatetv = (TextView)findViewById(R.id.textViewDateBirth);
birthDayDatePicker = (DatePicker)findViewById(R.id.DatePickerBirthDay);
periodDatePicker = (DatePicker)findViewById(R.id.DatePickerPeriod);
periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
{
#Override
public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
// TODO Auto-generated method stub
birthDateCalculations();
}
});
birthDayDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener () {
#Override
public void onDateChanged(DatePicker birthDayDatePicker, int currentYear, int currentMonth, int currentDay) {
// TODO Auto-generated method stub
periodDateCalculations();
}
});
}
As I mentioned before, the pickers are connected, so when I change one picker date the other is also changed so the two listeners for the two date pickers will be invoked,, I just want the listener that is involved with the picker that the user changed its date to be invoke ,, Any idea how to do that ?
Here is the methods that the listeners do if it may help
public void birthDateCalculations ()
{
Calendar start = Calendar.getInstance();
int periodYear = periodDatePicker.getYear();
int periodMonth = periodDatePicker.getMonth();
int periodDay = periodDatePicker.getDayOfMonth();
start.set(periodYear, periodMonth, periodDay);
birthDayDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
periodDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
Date periodDate = start.getTime();
int daysToAdd = 280;
Calendar cal = Calendar.getInstance();
cal.setTime(periodDate);
cal.add(Calendar.DAY_OF_MONTH,daysToAdd );
System.err.println("-----" +cal.getTime());
int birthYearAfterCalc = cal.getTime().getYear()+1900;
System.err.println("birthYearAfterCalc-----" + birthYearAfterCalc);
int birthMonthAfterCalc = cal.getTime().getMonth();
System.err.println("birthMonthAfterCalc----" + birthMonthAfterCalc);
int birthDayAfterCalc = cal.getTime().getDate();
System.err.println("birthDayAfterCalc"+birthDayAfterCalc);
//user edit period to get birth
if( periodDay <= currentDay && periodMonth <= currentMonth && periodYear <= currentYear){
//the program runs normally
birthDayDatePicker.updateDate(birthYearAfterCalc, birthMonthAfterCalc, birthDayAfterCalc);
}
else {
new AlertDialog.Builder(birthDate.this)
.setTitle("Wrong Data Input!")
.setMessage("Error in period date input")
.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
periodDatePicker.updateDate(currentYear,currentMonth, currentDay);
birthDayDatePicker.updateDate(currentYear, currentMonth, currentDay);
}
}
public void periodDateCalculations ()
{
Calendar start2 = Calendar.getInstance();
// get the chosen date from birth date picker
int birthYear = birthDayDatePicker.getYear();
int birthMonth = birthDayDatePicker.getMonth();
int birthDay = birthDayDatePicker.getDayOfMonth();
//set the chosen date to calendar instance
start2.set(birthYear, birthMonth, birthDay);
birthDayDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
periodDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
//get the date
Date birthDate = start2.getTime();
//int constrain = currentMonth + 9;
int daysToAdd = -280;
//another instance from calendar
Calendar cal2 = Calendar.getInstance();
//set birth date chosen to calendar
cal2.setTime(birthDate);
//add -280 day to birth day to get period date
cal2.add(Calendar.DAY_OF_MONTH,daysToAdd);
System.err.println("-----" +cal2.getTime());
int periodYearAfterCalc = cal2.getTime().getYear()+1900;
System.err.println("periodYearAfterCalc-----" + periodYearAfterCalc);
int periodMonthAfterCalc = cal2.getTime().getMonth();
System.err.println("periodMonthAfterCalc----" + periodMonthAfterCalc);
int periodDayAfterCalc = cal2.getTime().getDate();
System.err.println("periodDayAfterCalc"+periodDayAfterCalc);
//edit birth date picker from user
if(birthDay >= currentDay && birthMonth >= currentMonth && birthYear >= currentYear){
//the program runs normally
periodDatePicker.updateDate(periodYearAfterCalc, periodMonthAfterCalc, periodDayAfterCalc);
}
else{
new AlertDialog.Builder(birthDate.this)
.setTitle("Wrong Data Input!")
.setMessage("birth day input error")
.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
birthDayDatePicker.updateDate(currentYear,currentMonth, currentDay);
periodDatePicker.updateDate(currentYear, currentMonth, currentDay);
}
}
}
what you could do is set the corresponding listener to null when the other one takes action, and then after the task, you could turn it back on.
for example:
periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
{
#Override
public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
// TODO Auto-generated method stub
birthDayDatePicker.init(currentYear, currentMonth, currentYear, null);
birthDateCalculations();
restartBirthDayDatePickerListener();
// ^here you turn back on that init command that we have just set to null.
}
});
not a glamorous solution, but perhaps you can make heads or tails of it.

Categories