date picker display problem in android - java

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];

Related

How can I set the specific selected date to min date in android date picker? (java)

I have 2 DatePickers, one is start date picker and the other is end date picker. I want to set the min date of the end DatePicker to the start date which user has selected.
I set the Data which user selected in Calander startDate and set this data as MinDate at end date.
I've tried in this way but it's not working :(
I can't use Mateiral Range Date Picker because I have to use specific theme.
Also I'm not good at programming so I'd appreciate it if you could explain it in detail.
ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
final Calendar c = Calendar.getInstance();
startYear = c.get(Calendar.YEAR);
startMonth = c.get(Calendar.MONTH);
startDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
startYear = year;
startMonth = month+1;
startDay = dayOfMonth;
ipDcEventStartDay.setText(startYear + " - " + startMonth + " - " + startDay);
ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
}
}, startYear, startMonth, startDay);
dpd.show();
}
});
Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);
ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
endYear = c.get(Calendar.YEAR);
endMonth = c.get(Calendar.MONTH);
endDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
endYear = year;
endMonth = month;
endDay = dayOfMonth;
ipDcEventEndDay.setText(endYear + " - " + (endMonth+1) + " - " + endDay);
ipDcEventEndDay.setTextColor(Color.parseColor("#000000"));
}
}, endYear,endMonth, endDay);
dpd.getDatePicker().setMinDate(startDate.getTimeInMillis());
dpd.show();
}
});
This code:
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);
Should be inside
ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
Because startYear, startMonth and startDay are only set on ipDcEventStartDay.setOnClickListener(), so at the beginning they have some value but not the one that was set on ipDcEventStartDay.setOnClickListener()
So in ipDcEventEndDay.setOnClickListener() you have to set again on startDate the values that were picked on the other DatePickerDialog

How to set restrictions for DatePicker and TimePicker in Android

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

how i can show traditional calendar?

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

Setting "today date" in DatePicker by timezone (Android)

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

how to update edittext on datepicker set button [duplicate]

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.

Categories

Resources