This question already exists:
edittext not updated when set new date
Closed 9 years ago.
this is my code which work fine updare edittext but problem is edittext not update imediately until i click again on edittext tell me how do i update edittext on datepicker dailog (positive button) h date picker display two default button positive and negative how do i update editteext on positive button help me please
EditText edtKeyword;
String selectedDate;
int mYear,mMonth,mDay ;
DateFormat fmtDateAndTime = DateFormat.getDateInstance();
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
edtKeyword = (EditText) findViewById(R.id.edtKeyword);
edtKeyword.setClickable(true);
edtKeyword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(MenuList.this, d, myCalendar
.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
mYear = myCalendar.get(Calendar.YEAR);
mMonth = myCalendar.get(Calendar.MONTH);
mDay = myCalendar.get(Calendar.DAY_OF_MONTH);
selectedDate= mMonth+"-"+mDay+"-"+mYear;
edtKeyword.setText(selectedDate);
}
});
edtKeyword.setText(fmtDateAndTime.format(myCalendar.getTime()));
I tried this code. It works fine for me. Please try it once
private int pYear = 1970;
private int pMonth = 1;
private int pDay = 1;
static final int DATE_DIALOG_ID = 0;
Then in onCreate:
edt_birth.setKeyListener(null);
edt_birth.setOnTouchListener(new OnTouchListener() {
#SuppressWarnings("deprecation")
#Override
public boolean onTouch(View v, MotionEvent event) {
showDialog(DATE_DIALOG_ID);
return false;
}
});
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, pDateSetListener, pYear, pMonth,
pDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
pYear = year;
pMonth = monthOfYear;
pDay = dayOfMonth;
updateDisplay();
}
};
private void updateDisplay() {
int temp = pMonth + 1;
edt_birth.setText(pDay + "/" + temp + "/" + pYear);
}
This will update your edittext.
Related
I have already read this post but nothing helped me. The listener is still not called.
Here is my code:
private void callDatePicker() {
// получаем текущую дату
final Calendar cal = Calendar.getInstance();
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
dl = new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String editTextDateParam = dayOfMonth + "." + (monthOfYear + 1) + "." + year;
date_text.setText(editTextDateParam);
Log.d("BIMI", editTextDateParam);
}
};
// инициализируем диалог выбора даты текущими значениями
DatePickerDialog datePickerDialog = new DatePickerDialog(this, dl, mYear, mMonth, mDay);
datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
String editTextDateParam = mDay + "." + (mMonth + 1) + "." + mYear;
Log.d("GROM", editTextDateParam);
date_text.setText(editTextDateParam);
}
}
});
datePickerDialog.show();
}
callDatePicker is called in the AlertDialog when the button is clicked.
If you set both BUTTON_POSITIVE click listener and OnDateSetListener then only the positive button click will be called. If you want to call OnDateSetListener then don't set the button. If you need to change properties of a button like color,text then do it on setOnShowListener. So your final code will look something like this.
private void callDatePicker() {
// получаем текущую дату
int mYear,mMonth,mDay;
final Calendar cal = Calendar.getInstance();
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog.OnDateSetListener dl = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String editTextDateParam = dayOfMonth + "." + (monthOfYear + 1) + "." + year;
// date_text.setText(editTextDateParam);
Log.d("BIMI", editTextDateParam);
}
};
// инициализируем диалог выбора даты текущими значениями
DatePickerDialog datePickerDialog = new DatePickerDialog(this, dl, mYear, mMonth, mDay);
datePickerDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button pButton = datePickerDialog.getButton (DialogInterface.BUTTON_POSITIVE);
pButton.setText ("Set date");
}
});
datePickerDialog.show();
}
I have two date picker From and To, here i can set From datepicker value as minimum date of to datepicker.
Now my requirement is ,if i select To datepicker date, i want to set it as maximum date of From datepicker date. I tried one solution, but its not working in my case.
private String fromDate;
private DatePickerDialog fromDatePicker;
private DatePickerDialog.OnDateSetListener fromDatePickerListener;
private Calendar fromDateCalendar;
private String toDate;
private DatePickerDialog toDatePicker;
private DatePickerDialog.OnDateSetListener toDatePickerListener;
private Calendar toDateCalendar;
Fromdatepicker OnDateSetListener method
fromDateCalendar = Calendar.getInstance();
fromDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
fromDateCalendar.set(Calendar.YEAR, year);
fromDateCalendar.set(Calendar.MONTH, monthOfYear);
fromDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
fromDate = fromDateCalendar.get(Calendar.YEAR) + "-" +
(fromDateCalendar.get(Calendar.MONTH)+1) + "-" +
fromDateCalendar.get(Calendar.DAY_OF_MONTH);
textviewFrom.setText(fromDate);
toDatePicker.getDatePicker().setMinDate(fromDateCalendar.getTimeInMillis());
}
};
To datepicker OnDateSetListener method
toDateCalendar = Calendar.getInstance();
toDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
toDateCalendar.set(Calendar.YEAR, year);
toDateCalendar.set(Calendar.MONTH, monthOfYear);
toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
toDate = toDateCalendar.get(Calendar.YEAR) + "-" +
(toDateCalendar.get(Calendar.MONTH)+1) + "-" +
toDateCalendar.get(Calendar.DAY_OF_MONTH);
textviewTo.setText(toDate);
todatepicker.getDatePicker().setMinDate(-2208902400000L);
fromdatePicker.getDatePicker().setMaxDate(toDateCalendar.getTimeInMillis());
}
};
toDatePicker = new DatePickerDialog(getContext(),toDatePickerListener,
toDateCalendar.get(Calendar.YEAR), toDateCalendar.get(Calendar.MONTH),
toDateCalendar.get(Calendar.DAY_OF_MONTH));
fromDatePicker = new DatePickerDialog(getContext(),fromDatePickerListener,
fromDateCalendar.get(Calendar.YEAR), fromDateCalendar.get(Calendar.MONTH),
fromDateCalendar.get(Calendar.DAY_OF_MONTH));
Textview listner for call datepicker dialog
textviewFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fromDatePicker.show();
}
});
textviewTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toDatePicker.show();
}
});
I tried
fromDatePicker.getDatePicker().setMaxDate(toDateCalendar.getTimeInMillis());
in onDateSet method of toDatePickerListener, but its not
working
I have tried the following way,Hope it may help you.
private String selectedToDate = "";
private String selectedFromDate = "";
private Calendar calToDate, calFromDate;
Write this code in onCreate() method
calFromDate = convertToCalender(selectedFromDate);
calToDate = convertToCalender(selectedToDate);
Todate and FromDate TextView Clicks
textviewFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showFromDatePicker(fromdateListener, calFromDate, calToDate);
}
});
textviewTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showToDatePicker(todateListener, calToDate, calFromDate);
}
});
toDate and fromDate Listener
public DatePickerDialog.OnDateSetListener fromdateListener =
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calFromDate.set(year, monthOfYear, dayOfMonth);
selectedFromDate = returnFormattedDate(calFromDate.getTimeInMillis());
textviewFrom.setText(selectedFromDate);
}
};
DatePickerDialog.OnDateSetListener todateListener =
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calToDate.set(year, monthOfYear, dayOfMonth);
selectedToDate = returnFormattedDate(calToDate.getTimeInMillis());
textviewTo.setText(selectedToDate);
}
};
private String returnFormattedDate(long timeInMilliSec) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
return dateFormat.format(new Date(timeInMilliSec));
}
Methods to show DatePicker
private void showFromDatePicker(DatePickerDialog.OnDateSetListener listener,
Calendar calendar,
Calendar minDateCalender) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
if (minDateCalender != null) {
dialog.getDatePicker().setMinDate(minDateCalender.getTimeInMillis());
}
dialog.show();
}
private void showToDatePicker(DatePickerDialog.OnDateSetListener listener,
Calendar calendar,
Calendar maxDateCalender) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMaxDate(maxDateCalender.getTimeInMillis());
dialog.show();
}
Method to convert string date to Calender
private Calendar convertToCalender(String date) {
Calendar mCalender = Calendar.getInstance();
if (date == null) {
return mCalender;
} else {
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date returnDate = formatter.parse(date);
mCalender = Calendar.getInstance();
mCalender.setTime(returnDate);
return mCalender;
} catch (ParseException e) {
e.printStackTrace();
return mCalender;
}
}
}
If you are are using DatePicker widget in xml the following will work .
toDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
toDateCalendar.set(Calendar.YEAR, year);
toDateCalendar.set(Calendar.MONTH, monthOfYear);
toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
toDate = toDateCalendar.get(Calendar.YEAR) + "-" +
(toDateCalendar.get(Calendar.MONTH)+1) + "-" +
toDateCalendar.get(Calendar.DAY_OF_MONTH);
fromdatePicker.setMaxDate(toDateCalendar.getTimeInMillis());
//etToDate.setText(toDate);
}
};
Or if you opening DatePickerDialog oen after another . Then you should save the previously selected date and pass it to next DatePickerDialog before calling show.
pickerDialog.getDatePicker().setMaxDate(maxTime);// Set Max and min here
pickerDialog.show();
I am working on a simple app in which i have taken two date pickers they subtract two dates and display the result. But i want that if the two dates are same like 06/04/2017 then two radio button should pop up for selecting full day and half day as soon as it calculates that the two dates are same.
how to do that?
public class Leave extends AppCompatActivity {
TextView date;
private DatePickerDialog datePickerDialog;
TextView date2;
//TextView setDay;
private DatePickerDialog datePickerDialog2;
TextView no_of_days;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave);
date = (TextView) findViewById(R.id.date);
date2 = (TextView) findViewById(R.id.date2);
//setDay = (TextView) findViewById(R.id.setDay);
no_of_days = (TextView) findViewById(R.id.no_of_days);
// initiate the date picker and a button
date = (TextView) findViewById(R.id.date);
// perform click event on edit text
date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
// initiate the date picker and a button
date2 = (TextView) findViewById(R.id.date2);
// perform click event on edit text
date2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog2 = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
date2.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog2.show();
}
});
no_of_days.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
String date01 = date.getText().toString();
String date02 = date2.getText().toString();
try {
Date d = format.parse(date01);
Date d1 = format.parse(date02);
getDifferenceDays(d, d1);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.leave_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
public class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
SpinnerActivity() throws ParseException {
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
}
}
private void setupRadios() {
RadioButton radio_full = (RadioButton) findViewById(R.id.radio_full);
RadioButton radio_Half = (RadioButton) findViewById(R.id.radio_Half);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioleave);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int buttonId = radioGroup.getCheckedRadioButtonId();
switch (buttonId) {
case R.id.radio_full:
Toast.makeText(getApplicationContext(), "You have selected Full Day Leave", Toast.LENGTH_SHORT).show();
break;
case R.id.radio_Half:
Toast.makeText(getApplicationContext(), "You have selected Half Day Leave", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
public void getDifferenceDays(Date d1, Date d2) {
int daysdiff = 0;
long diff = d2.getTime() - d1.getTime();
long diffDays = diff / (24 * 60 * 60 * 1000) + 1;
daysdiff = (int) diffDays;
no_of_days.setText(Integer.toString(daysdiff));
System.out.println("day count=>" + daysdiff);
}
}
Compare the dates whenever any date is selected from any one of the date picker's as follows :
public class Leave extends AppCompatActivity {
TextView date;
private DatePickerDialog datePickerDialog;
TextView date2;
//TextView setDay;
private DatePickerDialog datePickerDialog2;
TextView no_of_days;
String date1 = "", date2 = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave);
date = (TextView) findViewById(R.id.date);
date2 = (TextView) findViewById(R.id.date2);
//setDay = (TextView) findViewById(R.id.setDay);
no_of_days = (TextView) findViewById(R.id.no_of_days);
RadioButton radio_full = (RadioButton) findViewById(R.id.radio_full);
RadioButton radio_Half = (RadioButton) findViewById(R.id.radio_Half);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioleave);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int buttonId = radioGroup.getCheckedRadioButtonId();
switch (buttonId) {
case R.id.radio_full:
Toast.makeText(getApplicationContext(), "You have selected Full Day Leave", Toast.LENGTH_SHORT).show();
break;
case R.id.radio_Half:
Toast.makeText(getApplicationContext(), "You have selected Half Day Leave", Toast.LENGTH_SHORT).show();
break;
}
}
});
// initially hide the radio buttons
radio_full.setVisibility(View.GONE);
radio_Half.setVisibility(View.GONE);
// initiate the date picker and a button
date = (TextView) findViewById(R.id.date);
// perform click event on edit text
date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
// assign the value to date1
date1 = dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year;
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
// check if date2 has been set and compare it with date1
if(!TextUtils.isEmpty(date2)) {
// get difference
getDifferenceDays(date, date2);
if(date1.equals(date2)) {
// pop up radio button
radio_full.setVisibility(View.VISIBLE);
radio_Half.setVisibility(View.VISIBLE);
} else {
// hide radio buttons
radio_full.setVisibility(View.GONE);
radio_Half.setVisibility(View.GONE);
}
}
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
// initiate the date picker and a button
date2 = (TextView) findViewById(R.id.date2);
// perform click event on edit text
date2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog2 = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
// assign the value to date2
date2 = dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year;
date2.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
// check if date2 has been set and compare it with date1
if(!TextUtils.isEmpty(date1)) {
// get difference
getDifferenceDays(date, date2);
if(date1.equals(date2)) {
// pop up radio button
radio_full.setVisibility(View.VISIBLE);
radio_Half.setVisibility(View.VISIBLE);
} else {
// hide radio buttons
radio_full.setVisibility(View.GONE);
radio_Half.setVisibility(View.GONE);
}
}
}
}, mYear, mMonth, mDay);
datePickerDialog2.show();
}
});
no_of_days.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
String date01 = date.getText().toString();
String date02 = date2.getText().toString();
try {
Date d = format.parse(date01);
Date d1 = format.parse(date02);
getDifferenceDays(d, d1);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.leave_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
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);
}
}
};
I have four Buttons that display the the date/time of calendars which are attributes of my activity. When a button is pressed, the according DatePickerDialog gets called which sets the values the users entered in the calendar object.
Though the identification of the calendars and dialogs seems correct, when I press "endDate" and click on ok in the dialog, startDate's text gets set.
Why does android behave like this and to fix this nonsense behaviour? (And yes, I cleaned and refreshed the project...)
Thank's in advance, you always surprise me guys :)
UPDATE
OK, now I got it, I set the MONTH in the onTimeSetListener fr startDate instead of the MINUTE; fixed the code
/UPDATE
Here's the listing:
this is a part of the attribute definition (I left out the Views)
static final int START_DATE_DIALOG_ID = 0;
static final int START_TIME_DIALOG_ID = 1;
static final int END_DATE_DIALOG_ID = 2;
static final int END_TIME_DIALOG_ID = 3;
this is the relevant part of the onCreate()
startDate = (Button)findViewById(R.id.startDate);
startTime = (Button)findViewById(R.id.startTime);
endDate = (Button)findViewById(R.id.endDate);
endTime = (Button)findViewById(R.id.endTime);
startCal = Calendar.getInstance();
endCal = Calendar.getInstance();
[......]
startDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(START_DATE_DIALOG_ID);
}
});
startTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(START_TIME_DIALOG_ID);
}
});
endDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(END_DATE_DIALOG_ID);
}
});
endTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(END_TIME_DIALOG_ID);
}
});
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case START_DATE_DIALOG_ID:
return new DatePickerDialog(this,
startDateSetListener,
startCal.get(Calendar.YEAR),
startCal.get(Calendar.MONTH),
startCal.get(Calendar.DAY_OF_MONTH));
case START_TIME_DIALOG_ID:
return new TimePickerDialog(this,
startTimeSetListener,
startCal.get(Calendar.HOUR_OF_DAY),
startCal.get(Calendar.MINUTE),
true); // is24hView
case END_DATE_DIALOG_ID:
return new DatePickerDialog(this,
endDateSetListener,
endCal.get(Calendar.YEAR),
endCal.get(Calendar.MONTH),
endCal.get(Calendar.DAY_OF_MONTH));
case END_TIME_DIALOG_ID:
return new TimePickerDialog(this,
endTimeSetListener,
endCal.get(Calendar.HOUR_OF_DAY),
endCal.get(Calendar.MINUTE),
true); // is24hView
}
return null;
}
private DatePickerDialog.OnDateSetListener startDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
startCal.set(Calendar.YEAR, year);
startCal.set(Calendar.MONTH, monthOfYear);
startCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(START_DATE_DIALOG_ID);
}
}
;
private DatePickerDialog.OnDateSetListener endDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
endCal.set(Calendar.YEAR, year);
endCal.set(Calendar.MONTH, monthOfYear);
endCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(START_DATE_DIALOG_ID);
}
}
;
private TimePickerDialog.OnTimeSetListener startTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
startCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
startCal.set(Calendar.MINUTE, minute);
updateDisplay(START_TIME_DIALOG_ID);
}
}
;
private TimePickerDialog.OnTimeSetListener endTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
endCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
endCal.set(Calendar.MINUTE, minute);
updateDisplay(END_TIME_DIALOG_ID);
}
}
;
// updates the date in the TextView
private void updateDisplay(int dialogId) {
switch(dialogId){
case START_DATE_DIALOG_ID:
startDate.setText(getDateFromCal(startCal));
break;
case START_TIME_DIALOG_ID:
startTime.setText(getTimeFromCal(startCal));
break;
case END_DATE_DIALOG_ID:
endDate.setText(getDateFromCal(endCal));
break;
case END_TIME_DIALOG_ID:
endTime.setText(getTimeFromCal(endCal));
break;
}
}
private String getDateFromCal(Calendar cal){
return new StringBuilder()
.append(cal.get(Calendar.DAY_OF_MONTH)) .append(".")
.append(cal.get(Calendar.MONTH) + 1) .append(".")
.append(cal.get(Calendar.YEAR)) .toString();
}
private String getTimeFromCal(Calendar cal){
StringBuilder sb = new StringBuilder();
int num = cal.get(Calendar.HOUR_OF_DAY);
if(num<10)
sb.append("0");
sb.append(num);
sb.append(":");
num = cal.get(Calendar.MINUTE);
if(num<10)
sb.append("0");
sb.append(num);
return sb.toString();
}
You're updating the wrong display (START_DATE_DIALOG_ID) from your listener endDateSetListener.
private DatePickerDialog.OnDateSetListener endDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
endCal.set(Calendar.YEAR, year);
endCal.set(Calendar.MONTH, monthOfYear);
endCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(START_DATE_DIALOG_ID);
}
}
;
Also - may I make a suggestion? It is very rare that a language or library is ever the problem; never assume that it is. Jeff Atwood (one of the guys that started StackOverflow) has an excellent article on the subject: First rule of programming: It's always your fault