From ae83786b3cf44368adec0f73824e64ef78f02c34 Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Tue, 19 Jul 2016 20:45:05 -0700 Subject: [PATCH] Bottom sheet time picker working --- .../editalarm/BaseTimePickerDialog.java | 45 +++++++++++++++++-- .../clock2/editalarm/EditAlarmActivity.java | 8 +++- .../res/layout/content_grid_layout_numpad.xml | 40 +++++------------ .../res/layout/content_numpad_time_picker.xml | 16 ++----- .../res/layout/dialog_time_picker_numpad.xml | 6 ++- app/src/main/res/values/dimens.xml | 4 ++ app/src/main/res/values/styles.xml | 13 ++++++ 7 files changed, 83 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/editalarm/BaseTimePickerDialog.java b/app/src/main/java/com/philliphsu/clock2/editalarm/BaseTimePickerDialog.java index 4860b77..d693e00 100644 --- a/app/src/main/java/com/philliphsu/clock2/editalarm/BaseTimePickerDialog.java +++ b/app/src/main/java/com/philliphsu/clock2/editalarm/BaseTimePickerDialog.java @@ -1,20 +1,22 @@ package com.philliphsu.clock2.editalarm; +import android.app.Dialog; +import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.LayoutRes; -import android.support.annotation.Nullable; -import android.support.v4.app.DialogFragment; +import android.support.design.widget.BottomSheetBehavior; +import android.support.design.widget.BottomSheetDialog; +import android.support.design.widget.BottomSheetDialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.Window; import butterknife.ButterKnife; /** * Created by Phillip Hsu on 7/16/2016. */ -public abstract class BaseTimePickerDialog extends DialogFragment { +public abstract class BaseTimePickerDialog extends BottomSheetDialogFragment { // TODO: Consider private access, and then writing package/protected API that subclasses // can use to interface with this field. @@ -33,6 +35,40 @@ public abstract class BaseTimePickerDialog extends DialogFragment { mCallback = callback; } + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Dialog dialog = super.onCreateDialog(savedInstanceState); + // We're past onCreate() in the lifecycle, so we can safely retrieve the host activity. + View view = LayoutInflater.from(getActivity()).inflate(contentLayout(), null); + /** + * Adds our view to a ViewGroup that has a BottomSheetBehavior attached. The ViewGroup + * itself is a child of a CoordinatorLayout. + * @see {@link BottomSheetDialog#wrapInBottomSheet(int, View, ViewGroup.LayoutParams)} + */ + dialog.setContentView(view); + // Bind this fragment, not the internal dialog! + ButterKnife.bind(this, view); + final BottomSheetBehavior behavior = BottomSheetBehavior.from((View) view.getParent()); + // When we collapse, collapse all the way. Do not be misled by the "docs" in + // https://android-developers.blogspot.com.au/2016/02/android-support-library-232.html + // when it says: + // "STATE_COLLAPSED: ... the app:behavior_peekHeight attribute (defaults to 0)" + // While it is true by default, BottomSheetDialogs override this default height. + // See http://stackoverflow.com/a/35634293/5055032 for an alternative solution involving + // defining a style that overrides the attribute. + behavior.setPeekHeight(0); + dialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialog) { + // Every time we show, show at our full height. + behavior.setState(BottomSheetBehavior.STATE_EXPANDED); + } + }); + + return dialog; + } + + /* @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -41,6 +77,7 @@ public abstract class BaseTimePickerDialog extends DialogFragment { ButterKnife.bind(this, view); return view; } + */ @Override public void onDestroyView() { diff --git a/app/src/main/java/com/philliphsu/clock2/editalarm/EditAlarmActivity.java b/app/src/main/java/com/philliphsu/clock2/editalarm/EditAlarmActivity.java index 45400b9..2b00840 100644 --- a/app/src/main/java/com/philliphsu/clock2/editalarm/EditAlarmActivity.java +++ b/app/src/main/java/com/philliphsu/clock2/editalarm/EditAlarmActivity.java @@ -42,6 +42,7 @@ import butterknife.OnClick; import static android.text.format.DateFormat.getTimeFormat; import static com.philliphsu.clock2.DaysOfWeek.SATURDAY; import static com.philliphsu.clock2.DaysOfWeek.SUNDAY; +import static com.philliphsu.clock2.util.KeyboardUtils.hideKeyboard; import static com.philliphsu.clock2.util.Preconditions.checkNotNull; /** @@ -313,12 +314,15 @@ public class EditAlarmActivity extends BaseActivity implements @OnClick(R.id.input_time) void openTimePicker() { + // Close the keyboard first, or else our dialog will be screwed up. + // If not open, this does nothing. + hideKeyboard(this); // Create a new instance each time we want to show the dialog. // If we keep a reference to the dialog, we keep its previous state as well. // So the next time we call show() on it, the input field will show the // last inputted time. - //NumpadTimePickerDialog.newInstance(this).show(getSupportFragmentManager(), TAG_TIME_PICKER); - ScrollingGridTimePickerDialog.newInstance(this, true).show(getSupportFragmentManager(), "tag"); + NumpadTimePickerDialog.newInstance(this).show(getSupportFragmentManager(), TAG_TIME_PICKER); + //ScrollingGridTimePickerDialog.newInstance(this, true).show(getSupportFragmentManager(), "tag"); } private void setWeekDaysText() { diff --git a/app/src/main/res/layout/content_grid_layout_numpad.xml b/app/src/main/res/layout/content_grid_layout_numpad.xml index e80f365..09506f2 100644 --- a/app/src/main/res/layout/content_grid_layout_numpad.xml +++ b/app/src/main/res/layout/content_grid_layout_numpad.xml @@ -3,72 +3,52 @@ xmlns:grid="http://schemas.android.com/apk/res-auto">