Created themed styles for system timepicker
This commit is contained in:
parent
fd6f313a58
commit
d31c8643e9
@ -0,0 +1,48 @@
|
|||||||
|
package com.philliphsu.clock2.dialogs;
|
||||||
|
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.app.TimePickerDialog;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.text.format.DateFormat;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.TimePicker;
|
||||||
|
|
||||||
|
import com.philliphsu.clock2.timepickers.BaseTimePickerDialog;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Phillip Hsu on 9/28/2016.
|
||||||
|
*/
|
||||||
|
public class SystemTimePickerDialog extends android.support.v4.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {
|
||||||
|
|
||||||
|
public static SystemTimePickerDialog newInstance(BaseTimePickerDialog.OnTimeSetListener l,
|
||||||
|
int hourOfDay, int minute, boolean is24HourMode) {
|
||||||
|
SystemTimePickerDialog dialog = new SystemTimePickerDialog();
|
||||||
|
return dialog;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@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);
|
||||||
|
|
||||||
|
// Create a new instance of TimePickerDialog and return it
|
||||||
|
return new TimePickerDialog(getActivity(), this, hour, minute,
|
||||||
|
DateFormat.is24HourFormat(getActivity()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
|
||||||
|
// http://stackoverflow.com/q/19452993/5055032
|
||||||
|
// BUG! This is also called when the dialog is dismissed, so clicking
|
||||||
|
// the 'Done' button will end up calling this twice!
|
||||||
|
if (view.isShown()) {
|
||||||
|
Log.d("dfljsdlfkj", "Calling onTimeSet");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,10 +1,12 @@
|
|||||||
package com.philliphsu.clock2.dialogs;
|
package com.philliphsu.clock2.dialogs;
|
||||||
|
|
||||||
|
import android.app.TimePickerDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v4.app.FragmentManager;
|
import android.support.v4.app.FragmentManager;
|
||||||
import android.text.format.DateFormat;
|
import android.text.format.DateFormat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.widget.TimePicker;
|
||||||
|
|
||||||
import com.philliphsu.clock2.R;
|
import com.philliphsu.clock2.R;
|
||||||
import com.philliphsu.clock2.timepickers.BaseTimePickerDialog;
|
import com.philliphsu.clock2.timepickers.BaseTimePickerDialog;
|
||||||
@ -19,6 +21,7 @@ public final class TimePickerDialogController extends DialogFragmentController<B
|
|||||||
|
|
||||||
private final BaseTimePickerDialog.OnTimeSetListener mListener;
|
private final BaseTimePickerDialog.OnTimeSetListener mListener;
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
|
private final FragmentManager mFragmentManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param context Used to read the user's preference for the style of the time picker dialog to show.
|
* @param context Used to read the user's preference for the style of the time picker dialog to show.
|
||||||
@ -26,19 +29,17 @@ public final class TimePickerDialogController extends DialogFragmentController<B
|
|||||||
public TimePickerDialogController(FragmentManager fragmentManager, Context context,
|
public TimePickerDialogController(FragmentManager fragmentManager, Context context,
|
||||||
BaseTimePickerDialog.OnTimeSetListener listener) {
|
BaseTimePickerDialog.OnTimeSetListener listener) {
|
||||||
super(fragmentManager);
|
super(fragmentManager);
|
||||||
|
mFragmentManager = fragmentManager;
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mListener = listener;
|
mListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void show(int initialHourOfDay, int initialMinute, String tag) {
|
public void show(int initialHourOfDay, int initialMinute, String tag) {
|
||||||
BaseTimePickerDialog dialog = null;
|
BaseTimePickerDialog dialog = null;
|
||||||
String numpadStyle = mContext.getString(R.string.number_pad);
|
final String numpadStyle = mContext.getString(R.string.number_pad);
|
||||||
String gridStyle = mContext.getString(R.string.grid_selector);
|
final String gridStyle = mContext.getString(R.string.grid_selector);
|
||||||
String prefTimePickerStyle = PreferenceManager.getDefaultSharedPreferences(mContext).getString(
|
String prefTimePickerStyle = PreferenceManager.getDefaultSharedPreferences(mContext)
|
||||||
// key for the preference value to retrieve
|
.getString(mContext.getString(R.string.key_time_picker_style), numpadStyle);
|
||||||
mContext.getString(R.string.key_time_picker_style),
|
|
||||||
// default value
|
|
||||||
numpadStyle);
|
|
||||||
if (prefTimePickerStyle.equals(numpadStyle)) {
|
if (prefTimePickerStyle.equals(numpadStyle)) {
|
||||||
dialog = NumpadTimePickerDialog.newInstance(mListener);
|
dialog = NumpadTimePickerDialog.newInstance(mListener);
|
||||||
} else if (prefTimePickerStyle.equals(gridStyle)) {
|
} else if (prefTimePickerStyle.equals(gridStyle)) {
|
||||||
@ -47,10 +48,16 @@ public final class TimePickerDialogController extends DialogFragmentController<B
|
|||||||
initialHourOfDay,
|
initialHourOfDay,
|
||||||
initialMinute,
|
initialMinute,
|
||||||
DateFormat.is24HourFormat(mContext));
|
DateFormat.is24HourFormat(mContext));
|
||||||
|
} else {
|
||||||
|
// Use system default
|
||||||
|
// TimePickerDialog sysDefDialog = new TimePickerDialog(
|
||||||
|
// mContext, new ForwardingOnTimeSetListener(mListener),
|
||||||
|
// initialHourOfDay, initialMinute, DateFormat.is24HourFormat(mContext));
|
||||||
|
// sysDefDialog.show();
|
||||||
|
SystemTimePickerDialog timepicker = new SystemTimePickerDialog();
|
||||||
|
timepicker.show(mFragmentManager, "dfsd");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
// We don't have a default case, because we don't need one; prefTimePickerStyle
|
|
||||||
// will ALWAYS match one of numpadStyle or gridStyle. As such, the dialog
|
|
||||||
// will NEVER be null.
|
|
||||||
show(dialog, tag);
|
show(dialog, tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,4 +69,23 @@ public final class TimePickerDialogController extends DialogFragmentController<B
|
|||||||
picker.setOnTimeSetListener(mListener);
|
picker.setOnTimeSetListener(mListener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The listener to set on system's default time picker.
|
||||||
|
*/
|
||||||
|
private static final class ForwardingOnTimeSetListener implements TimePickerDialog.OnTimeSetListener {
|
||||||
|
private final BaseTimePickerDialog.OnTimeSetListener mListener;
|
||||||
|
|
||||||
|
private ForwardingOnTimeSetListener(BaseTimePickerDialog.OnTimeSetListener listener) {
|
||||||
|
mListener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
|
||||||
|
Log.d(TAG, "Calling onTimeSet");
|
||||||
|
if (mListener != null) {
|
||||||
|
mListener.onTimeSet(view, hourOfDay, minute);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,14 +13,17 @@
|
|||||||
<style name="AppTheme" parent="BaseAppTheme.Light">
|
<style name="AppTheme" parent="BaseAppTheme.Light">
|
||||||
<item name="android:navigationBarColor">?android:colorPrimary</item>
|
<item name="android:navigationBarColor">?android:colorPrimary</item>
|
||||||
<item name="android:alertDialogTheme">@style/AlertDialogStyle</item>
|
<item name="android:alertDialogTheme">@style/AlertDialogStyle</item>
|
||||||
|
<item name="android:timePickerDialogTheme">@style/TimePickerDialogTheme</item>
|
||||||
</style>
|
</style>
|
||||||
<style name="AppTheme.Dark" parent="BaseAppTheme.Dark">
|
<style name="AppTheme.Dark" parent="BaseAppTheme.Dark">
|
||||||
<item name="android:navigationBarColor">?android:colorPrimary</item>
|
<item name="android:navigationBarColor">?android:colorPrimary</item>
|
||||||
<item name="android:alertDialogTheme">@style/AlertDialogStyle.Dark</item>
|
<item name="android:alertDialogTheme">@style/AlertDialogStyle.Dark</item>
|
||||||
|
<item name="android:timePickerDialogTheme">@style/TimePickerDialogTheme.Dark</item>
|
||||||
</style>
|
</style>
|
||||||
<style name="AppTheme.Black" parent="BaseAppTheme.Black">
|
<style name="AppTheme.Black" parent="BaseAppTheme.Black">
|
||||||
<item name="android:navigationBarColor">?android:colorPrimary</item>
|
<item name="android:navigationBarColor">?android:colorPrimary</item>
|
||||||
<item name="android:alertDialogTheme">@style/AlertDialogStyle.Black</item>
|
<item name="android:alertDialogTheme">@style/AlertDialogStyle.Black</item>
|
||||||
|
<item name="android:timePickerDialogTheme">@style/TimePickerDialogTheme.Black</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style name="AlertDialogStyle" parent="android:Theme.Material.Dialog.Alert">
|
<style name="AlertDialogStyle" parent="android:Theme.Material.Dialog.Alert">
|
||||||
@ -36,6 +39,49 @@
|
|||||||
<item name="android:background">@color/alert_dialog_background_color_black</item>
|
<item name="android:background">@color/alert_dialog_background_color_black</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
<style name="TimePickerDialogTheme" parent="android:Theme.Material.Light.Dialog.Alert">
|
||||||
|
<!--
|
||||||
|
~ Why didn't we just extend from AlertDialogStyle to get these properties?
|
||||||
|
~ (1) We require a different variant of the .Dialog.Alert theme as a parent.
|
||||||
|
~ The Light variant gives us white text in the header.
|
||||||
|
~ (2) We could extend from AlertDialogStyle and then attempt to style the header's
|
||||||
|
~ time text appearance, but that won't work; as of 9/28/2016,
|
||||||
|
~ the attribute headerSelectedTextColor cannot be referenced at all.
|
||||||
|
~ The rest of the header's text style seem to be changeable, using the
|
||||||
|
~ headerTimeTextAppearance and headerAmPmTextAppearance attributes,
|
||||||
|
~ but that's a lot of needless work.
|
||||||
|
-->
|
||||||
|
<item name="android:colorAccent">@color/colorAccent</item>
|
||||||
|
<item name="android:background">@color/alert_dialog_background_color</item>
|
||||||
|
<item name="android:timePickerStyle">@style/TimePickerDialogStyle</item>
|
||||||
|
</style>
|
||||||
|
<style name="TimePickerDialogTheme.Dark">
|
||||||
|
<item name="android:colorAccent">@color/colorAccentInverse</item>
|
||||||
|
<item name="android:background">@color/alert_dialog_background_color_inverse</item>
|
||||||
|
<item name="android:timePickerStyle">@style/TimePickerDialogStyle.Dark</item>
|
||||||
|
</style>
|
||||||
|
<style name="TimePickerDialogTheme.Black">
|
||||||
|
<item name="android:colorAccent">@color/colorAccentBlack</item>
|
||||||
|
<item name="android:background">@color/alert_dialog_background_color_black</item>
|
||||||
|
<item name="android:timePickerStyle">@style/TimePickerDialogStyle.Black</item>
|
||||||
|
</style>
|
||||||
|
<style name="TimePickerDialogStyle" parent="@android:style/Widget.Material.TimePicker">
|
||||||
|
<!--https://android.googlesource.com/platform/frameworks/base/+/lollipop-release/core/res/res/values/styles_material.xml-->
|
||||||
|
<item name="android:headerBackground">@color/alert_dialog_background_color</item>
|
||||||
|
<item name="android:numbersBackgroundColor">@color/alert_dialog_background_color</item>
|
||||||
|
<item name="android:numbersTextColor">@color/text_color_primary_dark</item>
|
||||||
|
</style>
|
||||||
|
<style name="TimePickerDialogStyle.Dark">
|
||||||
|
<item name="android:headerBackground">@color/alert_dialog_background_color_inverse</item>
|
||||||
|
<item name="android:numbersBackgroundColor">@color/alert_dialog_background_color_inverse</item>
|
||||||
|
<item name="android:numbersTextColor">@color/text_color_primary_dark</item>
|
||||||
|
</style>
|
||||||
|
<style name="TimePickerDialogStyle.Black">
|
||||||
|
<item name="android:headerBackground">@color/alert_dialog_background_color_black</item>
|
||||||
|
<item name="android:numbersBackgroundColor">@color/alert_dialog_background_color_black</item>
|
||||||
|
<item name="android:numbersTextColor">@color/text_color_primary_dark</item>
|
||||||
|
</style>
|
||||||
|
|
||||||
<style name="SelectableButton">
|
<style name="SelectableButton">
|
||||||
<item name="android:background">?selectableItemBackgroundBorderless</item>
|
<item name="android:background">?selectableItemBackgroundBorderless</item>
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user