Datepicker dialog box does not open on click - java

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();
}
};

Related

To store DateTime value into php and mysql

I am working on how to put Date value into my PHP/MySQL using android's date picker.
I want to make a Sharing Parking lot application that owner can register one's parking place with its information like operating time. Then user can use it with choosing the beginning and ending points.
I searched a lot but need more specific information.
How can I save operating time slot into PHP/MySQL?
The following is my android java code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.avtivity_date_time);
mText1 = (TextView) findViewById(R.id.text1);
mPickDate1 = (Button) findViewById(R.id.pickDate1);
mPickTime1 = (Button) findViewById(R.id.pickTime1);
mText2 = (TextView) findViewById(R.id.text2);
mPickDate2 = (Button) findViewById(R.id.pickDate2);
mPickTime2 = (Button) findViewById(R.id.pickTime2);
mPickDate1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID_1);
}
});
mPickDate2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID_2);
}
});
mPickTime1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID_1);
}
});
mPickTime2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID_2);
}
});
final Calendar c = Calendar.getInstance();
mYear1 = c.get(Calendar.YEAR);
mMonth1 = c.get(Calendar.MONTH);
mDay1 = c.get(Calendar.DAY_OF_MONTH);
mHour1 = c.get(Calendar.HOUR_OF_DAY);
mMinute1 = c.get(Calendar.MINUTE);
mYear2 = c.get(Calendar.YEAR);
mMonth2 = c.get(Calendar.MONTH);
mDay2 = c.get(Calendar.DAY_OF_MONTH);
mHour2 = c.get(Calendar.HOUR_OF_DAY);
mMinute2 = c.get(Calendar.MINUTE);
updateDisplay();
}
private void updateDisplay() {
mText1.setText(String.format("시작 : %d년 %d월 %d일 %d시 %d분", mYear1, mMonth1 + 1, mDay1, mHour1, mMinute1));
mText2.setText(String.format("종료 : %d년 %d월 %d일 %d시 %d분", mYear2, mMonth2 + 1, mDay2, mHour2, mMinute2));
}
private DatePickerDialog.OnDateSetListener mDateSetListener1 =
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear1 = year;
mMonth1 = monthOfYear;
mDay1 = dayOfMonth;
updateDisplay();
}
};
private DatePickerDialog.OnDateSetListener mDateSetListener2 =
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear2 = year;
mMonth2 = monthOfYear;
mDay2 = dayOfMonth;
updateDisplay();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener1 =
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour1 = hourOfDay;
mMinute1 = minute;
updateDisplay();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener2 =
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour2 = hourOfDay;
mMinute2 = minute;
updateDisplay();
}
};
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID_1:
return new DatePickerDialog(this, mDateSetListener1, mYear1, mMonth1, mDay1);
case TIME_DIALOG_ID_1:
return new TimePickerDialog(this, mTimeSetListener1, mHour1, mMinute1, false);
case DATE_DIALOG_ID_2:
return new DatePickerDialog(this, mDateSetListener2, mYear2, mMonth2, mDay2);
case TIME_DIALOG_ID_2:
return new TimePickerDialog(this, mTimeSetListener2, mHour2, mMinute2, false);
}
return null;
}
private Long getDateInMS(String stringDateTime) throws ParseException {
SimpleDateFormat simpledateformat1 = new SimpleDateFormat("yyyy MM dd hh mm");
SimpleDateFormat simpledateformat2 = new SimpleDateFormat("yyyy MM dd hh mm");
String formatdate1 = simpledateformat1.format("mYear1, mMonth1, mDay1, mHour1, mMinute1");
String formatdate2 = simpledateformat2.format("mYear12, mMonth2, mDay2, mHour2, mMinute2");
Date startdate = simpledateformat1.parse(formatdate1);
Date enddate = simpledateformat2.parse(formatdate2);
return null;
}

How to calculate age based on picked date in DatePickerDialog android?

How could i do this?
This is my code:
private void setDateTimeField () {
Calendar newCalendar = Calendar.getInstance();
mDatePickerDialog = new DatePickerDialog(AddBirthday.this, new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
addBirthdayDate.setText(dateFormatter.format(newDate.getTime()));
dateSelected = String.valueOf(dayOfMonth) + " /" + String.valueOf(monthOfYear + 1)
+ " /" + String.valueOf(year);
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
I would like now to calculate when is birthday of user when he chose the date in DatePickerDialog and storing it in integer type.
You could try with an easier code like this for example:
private int mDay;
private int mMonth;
private int mYear;
private EditText date;
private Calendar c = new Calendar();
...
//and in your onCreate method:
date = (EditText)findViewById(R.id.date_of_birth_as_text);
date.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID); //DATE_DIALOG_ID is the id of your DatePickerDialog - declared in your layout file
}
});
//default values of the year, month and date - will be changed after click on the EditText view
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
protected Dialog onCreateDialog(int id) {
switch(id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener= new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, monthOfYear);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
}
};
private void updateDisplay() {
Date currentDate = new Date();
int age = currentDate.getYear() - mYear;
date.setText( new StringBuilder().append("The user is ")
.append(age).append(" years old"));
}
int age = getCurrentYear() -datePicker.getYear() ;

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();
// }
}
}
}

Limit Time Range in Time Picker

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

Dates in all activity

Hi am using Date picker in more than three class with same validation. Instead I must write in one class and call that function in other classes when I required, is it possible Below code for Date picker.
public class MainActivity extends Activity {
private TextView tvDisplayDate;
private Button btnChangeDate;
private int myear;
private int mmonth;
private int mday;
static final int DATE_DIALOG_ID = 999;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCurrentDateOnView();
addListenerOnButton();
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
final Calendar c = Calendar.getInstance();
myear = c.get(Calendar.YEAR);
mmonth = c.get(Calendar.MONTH);
mday = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(mmonth + 1).append("-").append(mday).append("-")
.append(myear).append(" "));
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
DatePickerDialog _date = new DatePickerDialog(this, datePickerListener, myear,mmonth,
mday)
{
#Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
if (year < myear)
view.updateDate(myear, mmonth, mday);
if (monthOfYear < mmonth && year == myear)
view.updateDate(myear, mmonth, mday);
if (dayOfMonth < mday && year == myear && monthOfYear == mmonth)
view.updateDate(myear, mmonth, mday);
}
};
return _date;
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
myear = selectedYear;
mmonth = selectedMonth;
mday = selectedDay;
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(mmonth + 1)
.append("-").append(mday).append("-").append(myear)
.append(" "));
Date dateObject1 = new Date(myear - 1900, mmonth, mday);
Date dateObj2 = new Date(System.currentTimeMillis());
if(dateObject1.before(dateObj2) || dateObject1.equals(dateObj2)){
//the program runs normally
}
else{
new AlertDialog.Builder(MainActivity.this)
.setTitle("Wrong Data Input!")
.setMessage("The end Date must be Before the start Date, please insert new Date values")
.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
}
}
};
}
You could have a DateUtils class with a static method, which takes in the dates, along with the context. This method can do the date validations and build the AlertDialog accordingly. You can call this static method this way.
DateUtils.dateValidator(Date, Context);
This can be called in the onDateSet() of your OnDateSetListener for each DatePicker.

Categories