Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimePickerDialog CANCEL and OK button not showing

Tags:

android

I have a class that extends DialogFragment and inside this class I'm using the TimePickerDialog.

The time picker works but the problem is sometimes the CANCEL and OK buttons don't show, as shown in the images below.

The buttons work but are invisible.

Here is the code used for the time picker.

public static class TimePickerFragment extends android.support.v4.app.DialogFragment
        implements TimePickerDialog.OnTimeSetListener {

    // String used to set both Start and end times
    String startEndTime;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        Bundle b = getArguments();

        if (b != null) {
            startEndTime = b.getString("startEndTime");
        }


        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }


    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        // Do something with the time chosen by the user
        switch (startEndTime) {

            case "startTime":

                break;

            case "endTime":

                break;

            default:

                break;

        }

    }
}


public void showTimePickerDialog(String startEndTime) {

    TimePickerFragment timePickerFragment = new TimePickerFragment();

    Bundle b = new Bundle();

    b.putString("startEndTime", startEndTime);

    timePickerFragment.setArguments(b);

    timePickerFragment.show(context.getSupportFragmentManager(), "timePicker");

}

enter image description here enter image description here

like image 397
bsanehi Avatar asked Dec 28 '17 01:12

bsanehi


People also ask

How to use timetime picker dialog in Android?

Time Picker Dialog in Android 1 Create an empty activity project Create an empty activity Android Studio project and select Kotlin as the programming language. ... 2 Working with the activity_main.xml file The main layout of the application contains a button and TextView to preview the picked time. ... 3 Working with MainActivity.kt file

How to show the date picker dialog in spinner mode?

If specified, then show the date picker dialog in Spinner mode. There are some default values such as android.R.style.Theme_Holo_Dialog, you can also use your customized styles. OnDateSetListener onDateSetListener: This is a listener object which will be invoked when the user clicks the OK button in the date picker dialog.

What are the default values of datepickerdialog in Android?

There are some default values such as android.R.style.Theme_Holo_Dialog, you can also use your customized styles. OnDateSetListener onDateSetListener: This is a listener object which will be invoked when the user clicks the OK button in the date picker dialog. int year: The year value that is shown in the DatePickerDialog.

How to create a datepickerdialog from a calendar instance in Java?

Calendar now = Calendar.getInstance (); int year = now.get (java.util.Calendar.YEAR); int month = now.get (java.util.Calendar.MONTH); int day = now.get (java.util.Calendar.DAY_OF_MONTH); // Create the new DatePickerDialog instance.


2 Answers

I fixed the issue, I think for some reasons the font color for the buttons were showing as white sometimes, have no idea why.

To fix the issue, I added a dialog theme to the TimePickerDialog to set the button text color.

Here is the xml code I added to styles.xml

<style name="MyTimePickerDialogTheme" parent="@style/Theme.AppCompat.Light.Dialog">
     <item name="android:textColor">@color/colorAccent</item>
     <item name="colorAccent">@color/colorAccent</item>
</style>

And here is the code I changed.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    Bundle b = getArguments();

    if (b != null) {
        startEndTime = b.getString("startEndTime");
    }


    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), R.style.MyTimePickerDialogTheme, this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
like image 83
bsanehi Avatar answered Oct 22 '22 09:10

bsanehi


For me the color of ok and cancel button were white , it was because my colorOnPrimary was set to white .

<item name="colorOnPrimary">@color/white</item>

And Timepicker used it , to fix that you can change that to black or any color you want.

<item name="colorOnPrimary">@color/black</item>

Or just add a new colorOnPrimary inside a custom theme and use it for timepicker.

<style name="MyTimePickerDialogTheme" parent="@style/Theme.AppCompat.Light.Dialog">
 <item name="colorOnPrimary">@color/black</item>
</style>
like image 34
Sachin Das Avatar answered Oct 22 '22 10:10

Sachin Das