I want to use digital datepicker dialog and timepicker dialog, I have writte the code and my picker dialogs are working but I want them in another format.
Currently my time picker and date picker dialog are in this format:
I want them like this:
Here is my code:
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.linearDate: {
DatePickerDialog dp = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mDatetext.setText(dayOfMonth+"-"+monthOfYear+"-"+year);
}
},year,month,day);
dp.show();
}break;
case R.id.linearTime: {
TimePickerDialog Tp = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mTimetext.setText(hourOfDay+ ":" +minute);
}
},hour,min,false);
Tp.show();
}break;
case R.id.linearPlace:{ startActivityForResult(new Intent(this,Location.class),CITY);
}break;
}
}
its very simple and i got my code working , i need to add theme and its done
case R.id.linearDate: {
DatePickerDialog dp = new DatePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mDatetext.setText(dayOfMonth+"-"+monthOfYear+"-"+year);
}
},year,month,day);
dp.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dp.show();
}break;
case R.id.linearTime: {
TimePickerDialog Tp = new TimePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mTimetext.setText(hourOfDay+ ":" +minute);
}
},hour,min,false);
Tp.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Tp.show();
}break;
I figured out how to use the digital TimePicker without using a Dialog. They implemented a feature that allows you to simply set the timePickerMode
attribute for the TimePicker in the layout resource file to either spinner
or clock
.
Source: https://gitlab.com/SaberMod/pa-android-frameworks-base/commit/3053b2fdcf7486f2e2f572f9b05ce65dacdd2b4c
Although this is a late answer but there is a bug for TimePickerDialog theme in Nougat device. I was not able to find the solution until i find this fix and this may help someone searching for same.
Create a custom TimePickerDialogFixedNougatSpinner class that extends TimePickerDialog
and customize it as below:-
public TimePickerDialogFixedNougatSpinner(Context context, int themeResId, OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView) {
super(context, themeResId, listener, hourOfDay, minute, is24HourView);
fixSpinner(context, hourOfDay, minute, is24HourView);
}
private void fixSpinner(Context context, int hourOfDay, int minute, boolean is24HourView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // android:timePickerMode spinner and clock began in Lollipop
try {
// Get the theme's android:timePickerMode
final int MODE_SPINNER = 2;
Class<?> styleableClass = Class.forName("com.android.internal.R$styleable");
Field timePickerStyleableField = styleableClass.getField("TimePicker");
int[] timePickerStyleable = (int[]) timePickerStyleableField.get(null);
final TypedArray a = context.obtainStyledAttributes(null, timePickerStyleable, android.R.attr.timePickerStyle, 0);
Field timePickerModeStyleableField = styleableClass.getField("TimePicker_timePickerMode");
int timePickerModeStyleable = timePickerModeStyleableField.getInt(null);
final int mode = a.getInt(timePickerModeStyleable, MODE_SPINNER);
a.recycle();
if (mode == MODE_SPINNER) {
TimePicker timePicker = (TimePicker) findField(TimePickerDialog.class, TimePicker.class, "mTimePicker").get(this);
Class<?> delegateClass = Class.forName("android.widget.TimePicker$TimePickerDelegate");
Field delegateField = findField(TimePicker.class, delegateClass, "mDelegate");
Object delegate = delegateField.get(timePicker);
Class<?> spinnerDelegateClass;
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.LOLLIPOP) {
spinnerDelegateClass = Class.forName("android.widget.TimePickerSpinnerDelegate");
} else {
// TimePickerSpinnerDelegate was initially misnamed TimePickerClockDelegate in API 21!
spinnerDelegateClass = Class.forName("android.widget.TimePickerClockDelegate");
}
// In 7.0 Nougat for some reason the timePickerMode is ignored and the delegate is TimePickerClockDelegate
if (delegate.getClass() != spinnerDelegateClass) {
delegateField.set(timePicker, null); // throw out the TimePickerClockDelegate!
timePicker.removeAllViews(); // remove the TimePickerClockDelegate views
Constructor spinnerDelegateConstructor = spinnerDelegateClass.getConstructor(TimePicker.class, Context.class, AttributeSet.class, int.class, int.class);
spinnerDelegateConstructor.setAccessible(true);
// Instantiate a TimePickerSpinnerDelegate
delegate = spinnerDelegateConstructor.newInstance(timePicker, context, null, android.R.attr.timePickerStyle, 0);
delegateField.set(timePicker, delegate); // set the TimePicker.mDelegate to the spinner delegate
// Set up the TimePicker again, with the TimePickerSpinnerDelegate
timePicker.setIs24HourView(is24HourView);
timePicker.setCurrentHour(hourOfDay);
timePicker.setCurrentMinute(minute);
timePicker.setOnTimeChangedListener(this);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
For more reference follow this link :- https://gist.github.com/jeffdgr8/6bc5f990bf0c13a7334ce385d482af9f
Make sure to pass MODE_SPINNER = 2 in fixSpinner function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With