I want to change my date picker view to standard mode
From
To
My code is
dateOfBirthET = (EditText) findViewById(R.id.dateOfBirth);
//setting dateSetListener
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
updateLabelToSave();
}
};
//setting onClickListener on setDate
dateOfBirthET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
DatePickerDialog dpd = new DatePickerDialog(RegisterActivity.this, date,
myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
//setting maxDate on tempCal
long maxDate = new Date().getTime();
tempCal.setTimeInMillis(maxDate);
tempCal.set(Calendar.YEAR, tempCal.get(Calendar.YEAR) - 16);
dpd.getDatePicker().setMaxDate(tempCal.getTimeInMillis());
dpd.show();
}
});
}
I am tried this code also but not working
dpd.getDatePicker().setCalendarViewShown(false);
You just need to change theme you want while creating DatePickerDialog instance
DatePickerDialog dpd = new DatePickerDialog(RegisterActivity.this, android.R.style.Theme_Holo_Dialog ,date,
myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
Working for me tested android 6.0 marshmallow.
From the setCalendarViewShown doc it says
Calling this method has no effect when the DatePicker_datePickerMode
attribute is set to calendar.
And as of Android L, with the Material theme selected, the default layout of android:datePickerMode is calendar (see here)
So if you want to change your DatePickerDialog to spiner style, you must change android:datePickerMode
And now I see datePickerMode can not change programmatically, it can only change by config XML like
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
/>
Therefore I think you should create a custom your DatePickerDilog layout
Related
My app uses a DatePicker. When I open the DatePicker and click on Done the date gets choosen. The problem is, that the date also gets choosen, if the user clicks besides the DatePicker. See here:
The Listener should only react if the Done Button is clicked. But it also reacts if the surrounding (red) of the DatePicker is clicked.
My Question:
How do I prevent this?
If you want to see some code, pls leave a comment.
I already tried this: How to handle date picker dialog not to set in edittext when clicked outside of dialog android?
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
//DatePickerDialog mDatePicker;
DatePickerDialog mDatePicker;
mDatePicker = new DatePickerDialog(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, new DatePickerDialog.OnDateSetListener() {
private boolean fired;
public void resetFired(){
fired = false;
}
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if(view.isShown()){
if(fired){
setDate(year, monthOfYear, dayOfMonth);
//ListView erneuern
adapter.notifyDataSetChanged();
return;
}
fired = true;
}
}
}, mYear, mMonth, mDay);
mDatePicker.setCanceledOnTouchOutside(true);
mDatePicker.setTitle("Wähle ein Datum");
mDatePicker.show();
But it does not work correctly. The click on the background bug gets solved. But now the Done Button stops working.
A simple dialog containing an DatePicker.
So You can set to no-cancelable.
using setCancelable API
.setCancelable(false);
Dialog reference
I need to open a DatePicker with a default date based on the YEAR-MONTH-DAY_OF_MONTH properties of a GregorianCalendar.
Here is the code where I open the DatePicker:
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
For exemple, if my values are like this:
MyCalendar.YEAR = 2017
MyCalendar.MONTH = 2
MyCalendar.DAY_OF_MONTH = 22
The default value set when I open the DatePicker would be:
What do I have to add to do that?
Basically straight from Android | Pickers
Plus, just like any other Fragment, you can use set and get-Arguments to pass data into the fragment.
Details: Best practice for instantiating a new Android Fragment
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public static DatePickerFragment newInstance(int year,int month,int day) {
Bundle b = new Bundle();
b.putInt("year", year);
// put others...
Fragment f = new DatePickerFragment();
f.setArguments(b);
return f;
}
#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);
// Update using the arguments
Bundle args = getArguments();
if (args != null) {
year = args.getInt("year");
// get others...
}
// 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) {
// Do something with the date chosen by the user
}
}
And use that newInstance method.
DialogFragment newFragment = DatePickerFragment.newInstance(2017,02,07);
newFragment.show(getFragmentManager(), "datePicker");
So I need for user to select date from DatePicker Dialog and then when he selects date I want it to be shown on Calendar. For example, if user selects 01/01/2016 I want that date to be shown as some red marked date.
Here's my code:
displayDatumaDodajPisaniIspit = (TextView)dialog. findViewById(R.id.displayDatumaDodajPisaniIspit);
set = (Button)dialog. findViewById(R.id.set);
set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(HomeScreen.this, listener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
And the listener
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
displayDatumaDodajPisaniIspit.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
calendarView.setDateSelected();
}
};
I don't have idea how to start with it. Tried with some methods in listener but it didn't work.
You should first convert the chosen date into millis -
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
long millis = calendar.getTimeInMillis();
and then set the date in your CalendarView like this -
calendarView.setDate(millis);
As you mentioned you are using MaterialCalendarView, then I guess you can use -
calendarView.setSelectedDate(CalendarDay.from(year,monthOfYear,dayOfMonth));
I am trying to use a DatePicker:
My code is:
DatePicker datePicker = (DatePicker) v.findViewById(R.id.dialog_date_datePicker);
datePicker.init(year, month, day, new OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year, int month,
int day) {
mDate = new GregorianCalendar(year, month, day).getTime();
getArguments().putSerializable(EXTRA_DATE, mDate);
}
});
But when I select a date the onDateChanged method is never called. After googling I found that if I changed my date picker layout to include: android:datePickerMode="spinner" /> and remove android:calendarViewShown="false" the onDateChanged is called and the code works but the date picker dialog is really bad looking.
My layout is:
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_date_datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:datePickerMode="spinner" />
<!-- android:calendarViewShown="false" -->
So why is the calenderViewShown attribute causing a problem and how can I improve the look of the date picker UI dialog?
If you want to use datepicker dialog than there's no need to use DatePicker just do it this way:
Calendar c = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(SignUpActivity.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth, new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
try {
Date d = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).parse(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setCalendarViewShown(false);
datePickerDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
datePickerDialog.show();
You can also use this library : Beautiful DatePicker UI
If on AS, add this in dependencies : compile 'com.github.flavienlaurent.datetimepicker:library:0.0.2'
Also,as pointed at this question : Datepicker
You can use this to solve the issue.
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_date_datePicker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="false"
android:datePickerMode="spinner" >
</DatePicker>
I want to create a textedit field where the user enters a date only (no time). The date will get stored in MY SQL. Whats the best way to do this with the least amount of validation? Is there like a built in textfield for dates that keeps it in the proper format?
I have this:
public static void AddEditTextDate(Context context, LinearLayout linearlayout, String text, int id) {
EditText edittext = new EditText(context);
edittext.setInputType(InputType.TYPE_DATETIME_VARIATION_DATE);
edittext.setText(text);
edittext.setId(id);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
edittext.setLayoutParams(params);
linearlayout.addView(edittext);
}
But when I try to type in it, it looks like a normal keyboard. I would expect it to go into the number pad by default or something...
EDIT: It needs to work with android 2.1+ (i.e. v7)
Does anyone know?
Thanks
You said Whats the best way to do this with the least amount of validation? Is there like a built in textfield for dates that keeps it in the proper format?
There is one way which comes to my mind, using which you might not need to check any validation of the date format that user entered. You can call a DatePickerDialog on click of your EditText box. Then user can choose the date using that. After user has chosen the date you can update your EditText with the chosen date. This way you lesses the effort of validation of the date format entered and user can easily and intuitively choose the date. You might so something like:
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
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);
updateLabel();
}
};
//When the editText is clicked then popup the DatePicker dialog to enable user choose the date
edittext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(new_split.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
// Call this whn the user has chosen the date and set the Date in the EditText in format that you wish
private void updateLabel() {
String myFormat = "MM/dd/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
edittext.setText(sdf.format(myCalendar.getTime()));
}
Source: This answer on Datepicker: How to popup datepicker when click on edittext question. Hope this helps.