Is it possible to customize the standart time picker as in the below picture?
The time picker i want to make
I can create my own time picker with the below code:
mTimePicker = TimePickerDialog(this, AlertDialog.THEME_HOLO_DARK , {
_, hourOfDay, min -> selectedTime.text = String.format("%d : %d", hourOfDay, min) },
hour, minute, true)
selectedTime.setOnClickListener {
mTimePicker.show()
}
And result:
My own Timepicker
I need to customize time picker somehow as in the first picture.
How can i do that?
You can create a custom DialogFragment, add a timePicker an the rest of the ui components such as buttons and the switch.
Related
I am new in android, and I tried to add time picker dialog to my app to take the time from the user.
but the dialog was not appear.
I am using android studio.
this is the code:
Calendar calendar=Calendar.getInstance();
int hour= calendar.get(Calendar.HOUR_OF_DAY);
int minutes1= calendar.get(Calendar.MINUTE);
EditText time=(EditText) findViewById(R.id.TimeEdT);
time.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TimePickerDialog timePickerDialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText(hourOfDay+":"+minute);
}
},hour, minutes1, true
);
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
});
the dialog is not appear when i tried to run the application.
my goal is to take the time from the user and use it as input to text, then use it for alarm notification.
please help
Your time View is an EditText.
When you click in an EditText it puts the cursor within the text allowing you to edit it, bringing up a soft keyboard if required.
It seems you want to get rid of that functionality and bring up a TimePickerDialog instead when you click on it. So if you don't need that extra functionality, just use a TextView instead.
I am trying to create a time picker for my application..
and when setting the theme to Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth
I am getting this output
I am getting a blank space around the dialog box
This is not happening when I am setting it to use the parent activity's theme which is appcompat.noactionbar
//not happening here
return new TimePickerDialog(getActivity(),this, hour, minute,is24HourView );
Here is my timepicker code
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
//Use the current time as the default values for the time picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
boolean is24HourView=true;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String formattedTime = sdf.format(c.getTime());
Log.d("time", formattedTime);
//Create and return a new instance of TimePickerDialog
// return new TimePickerDialog(getActivity(),this, hour, minute,is24HourView );
return new TimePickerDialog(getActivity(), android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth,this, hour, minute,is24HourView );
}
I know this is old, but here is a solution.
You could make a custom theme in styles.xml with your chosen theme as a parent, and set it's android:windowBackground attribute to #android:color/transparent:
<style name="TimePickerDialogStyle" parent="#android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar.MinWidth">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
Then pass your custom theme to the TimePickerDialog constructor:
new TimePickerDialog(getActivity(), R.style.TimePickerDialogStyle, this, hour, minute, is24HourView);
Change:
android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth
with
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT
or
AlertDialog.THEME_HOLO_LIGHT
It should work
I'm working with a DatePicker and finding that under Android 5.0 it will not call the OnDateChanged() method in its OnDateChangedListener when it's in CalendarView mode even though a new date has been selected. If android:datePickerMode="spinner" is set in the DatePicker's xml tag, the DatePicker will appear as spinners and it will call OnDateChanged() when a new date is selected. In earlier versions of Android, a DatePicker calls OnDateChanged() when a new date is selected in both CalendarView and Spinners versions. Here's the relevant code:
#SuppressLint("InflateParams")
View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);
DatePicker datePicker = (DatePicker) v.findViewById(R.id.dialog_date_DatePicker);
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
//Translate year, month, day into a Date object using a calendar
mDate = new GregorianCalendar(year, month, day).getTime();
//Update argument to preserve selected value on rotation
getArguments().putSerializable(EXTRA_DATE, mDate);
}
});
In my application, onDateChanged() doesn't get called and mDate doesn't get changed if the DatePicker is in CalendarView mode under Lollipop, but OnDateChanged() does get called and mDate does change if the DatePicker is in Spinners mode. Under earlier versions of Android, OnDateChanged() gets called and mDate gets changed in both versions of the DatePicker.
Is there any way to get a CalendarView DatePicker in 5.0 to call OnDateChanged()? Failing that, how else can I retrieve a changed date from the DatePicker when it's in CalendarView mode?
I face the same issue and the thing is onDateChange() and onTimeSet() listeners for DatePicker and TimePicker is not called in Nexus devices with lollipop Update.
The reason is in nexus devices, since the clock app is updated, the listeners are not working.
The work around is once the dialog dismiss, you need to create you own listener and set the values in a calendar object using the datepicker get() methods and pass the calendar to the listener.
A simple sample code is
/**
* Returns the calendar instance once the date and time is set.
* #return
*/
public Calendar getDateTime() {
mCalendar.set(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
return mCalendar;
}
Set this in your Xml-Layout:
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:spinnersShown="false"
android:id="#+id/datepicker_popupwindow"/>
With "android:spinnersShown="false" " you tell it to not show the Spinner. But it will call the 'onDateChanged' Method.
Implementing the DatePicker or DatePickerDialog in Android is easy. But when it comes to data storage, I have a problem with those classes:
If you use the spinners (+ or - button) to change the date, everything works fine. The event "Date changed" or "Date set" is called and you can get the values that the user entered.
But when the year is manually entered into the input field (via keyboard) and the user then clicks "Save" in the dialog, there won't be any event called and you won't get that manually entered value.
It only works when the user changes something with the sliders again after manually entering the year. Because when you use the sliders, the events are fired.
Is this normal behaviour? How can I achieve the desired behaviour, namely that an event is fired when the user enteres something manually and then clicks "Save"?
Thanks in advance!
Just clear focus, and android will set the number from manual input.
eg:
DatePicker datePicker = findViewById(R.id.dp);
When saving just like onClick(), add
datePicker.clearFocus();
This must be working.
I had the same problem and the accepted answer really helped me a lot. My situation is a little different though as I'm using DatePickerDialog. Here's how the DatePicker properly worked finally.
Declare the variables first and then define them.
private DatePickerDialog.OnDateSetListener date;
private DatePickerDialog mDatePickerDialog;
private Calendar myCalendar;
// Get the calendar instance
myCalendar = Calendar.getInstance();
// Define the date set listener first.
date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// Do something with year, monthOfYear and dayOfMonth
}
};
// Now define the DatePickerDialog
mDatePickerDialog = new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
mDatePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Set", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
DatePicker datePicker = mDatePickerDialog.getDatePicker();
// The following clear focus did the trick of saving the date while the date is put manually by the edit text.
datePicker.clearFocus();
date.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
}
});
Then inside an onClick of a button
mDatePickerDialog.show();
If you look at the Date Picker Example, the DatePickerDialog.OnDateSetListener callback is received as soon as the user clicks the 'SET' button on the dialog.
Look at the dialog below
Even if you enter the date using the keyboard, the date itself is not accepted until you click the 'SET" button in the dialog and that is when the DatePickerDialog.OnDateSetListener callback is called.
I'm using a datePicker dialog to pick the date, and I must set a lower and an upper bound. How can i do this?
Should I use the public void onDateChanged(DatePicker view, int year, int month, int day) method?
Please be explicit because I'm working with Android from 4 days only!
I suppose a DatePicker can NOT have a bound range. But you can check the date picked by user yourself. And if the date is invalid, you can alert the user.
You can set the maxDate and minDate like this:
Date upperLimit = new Date();
Date lowerLimit = new Date();
DatePicker picker = new DatePicker(getContext());
picker.setMaxDate(upperLimit.getTime());
picker.setMinDate(lowerLimit.getTime());
Cheers