Change LabelDialogFragment to use AlertDialog.
Bug: 21638785
Change-Id: I72828cd5812b7d4f0dfc866f9df12af9460181d0
diff --git a/res/layout/label_dialog.xml b/res/layout/label_dialog.xml
deleted file mode 100644
index 6d850f6..0000000
--- a/res/layout/label_dialog.xml
+++ /dev/null
@@ -1,76 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-
-<!--
- ~ Copyright (C) 2012 The Android Open Source Project
- ~
- ~ Licensed under the Apache License, Version 2.0 (the "License");
- ~ you may not use this file except in compliance with the License.
- ~ You may obtain a copy of the License at
- ~
- ~ http://www.apache.org/licenses/LICENSE-2.0
- ~
- ~ Unless required by applicable law or agreed to in writing, software
- ~ distributed under the License is distributed on an "AS IS" BASIS,
- ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ~ See the License for the specific language governing permissions and
- ~ limitations under the License
- -->
-
-<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
- <TextView
- android:id="@+id/title"
- style="?android:attr/windowTitleStyle"
- android:layout_marginTop="24dip"
- android:layout_marginLeft="16dip"
- android:layout_marginRight="16dip"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="4dip"
- android:text="@string/label"
- android:textColor="@color/white" />
-
- <EditText
- android:id="@+id/labelBox"
- style="@style/labelEditTextStyle"
- android:layout_marginTop="16dip"
- android:layout_marginBottom="24dip"
- android:layout_marginLeft="16dip"
- android:layout_marginRight="16dip"
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:layout_below="@id/title" />
-
- <LinearLayout
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:layout_marginLeft="16dip"
- android:layout_marginRight="16dip"
- android:layout_marginBottom="16dip"
- android:layout_below="@id/labelBox"
- android:gravity="end">
-
- <Button
- android:id="@+id/cancelButton"
- style="?android:attr/borderlessButtonStyle"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="@string/time_picker_cancel"
- android:textAllCaps="true"
- android:textColor="@color/hot_pink"
- android:textSize="@dimen/dialog_button_font_size" />
-
- <Button
- android:id="@+id/setButton"
- style="?android:attr/borderlessButtonStyle"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="@string/time_picker_set"
- android:textAllCaps="true"
- android:textColor="@color/hot_pink"
- android:textSize="@dimen/dialog_button_font_size" />
-
- </LinearLayout>
-</RelativeLayout>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index ac574da..822003f 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -45,6 +45,10 @@
<item name="android:textSize">16sp</item>
</style>
+ <style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
+ <item name="colorAccent">@color/hot_pink</item>
+ </style>
+
<style name="DeskClock" parent="@style/DeskClockParentTheme">
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@color/default_background</item>
diff --git a/src/com/android/deskclock/LabelDialogFragment.java b/src/com/android/deskclock/LabelDialogFragment.java
index 9d61eb4..58a2f32 100644
--- a/src/com/android/deskclock/LabelDialogFragment.java
+++ b/src/com/android/deskclock/LabelDialogFragment.java
@@ -17,21 +17,20 @@
package com.android.deskclock;
import android.app.Activity;
+import android.app.Dialog;
import android.app.DialogFragment;
+import android.content.Context;
+import android.content.DialogInterface;
import android.os.Bundle;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
-import android.widget.Button;
-import android.widget.EditText;
import android.widget.TextView;
-import android.widget.TextView.OnEditorActionListener;
import com.android.deskclock.provider.Alarm;
import com.android.deskclock.timer.TimerObj;
@@ -46,7 +45,7 @@
private static final String KEY_TIMER = "timer";
private static final String KEY_TAG = "tag";
- private EditText mLabelBox;
+ private AppCompatEditText mLabelBox;
public static LabelDialogFragment newInstance(Alarm alarm, String label, String tag) {
final LabelDialogFragment frag = new LabelDialogFragment();
@@ -71,23 +70,21 @@
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setStyle(DialogFragment.STYLE_NO_TITLE, 0);
}
@Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bundle = getArguments();
final String label = bundle.getString(KEY_LABEL);
final Alarm alarm = bundle.getParcelable(KEY_ALARM);
final TimerObj timer = bundle.getParcelable(KEY_TIMER);
final String tag = bundle.getString(KEY_TAG);
- final View view = inflater.inflate(R.layout.label_dialog, container, false);
+ final Context context = getActivity();
- mLabelBox = (EditText) view.findViewById(R.id.labelBox);
+ mLabelBox = new AppCompatEditText(context);
mLabelBox.setText(label);
- mLabelBox.setOnEditorActionListener(new OnEditorActionListener() {
+ mLabelBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
@@ -111,28 +108,30 @@
public void afterTextChanged(Editable editable) {
}
});
+ mLabelBox.selectAll();
setLabelBoxBackground(TextUtils.isEmpty(label));
- final Button cancelButton = (Button) view.findViewById(R.id.cancelButton);
- cancelButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- dismiss();
- }
- });
+ final AlertDialog alertDialog = new AlertDialog.Builder(context, R.style.AlertDialogTheme)
+ .setView(mLabelBox)
+ .setPositiveButton(R.string.time_picker_set, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ set(alarm, timer, tag);
+ }
+ })
+ .setNegativeButton(R.string.time_picker_cancel,
+ new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ dismiss();
+ }
+ })
+ .setMessage(R.string.label)
+ .create();
- final Button setButton = (Button) view.findViewById(R.id.setButton);
- setButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- set(alarm, timer, tag);
- }
- });
-
- getDialog().getWindow().setSoftInputMode(
+ alertDialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
-
- return view;
+ return alertDialog;
}
private void set(Alarm alarm, TimerObj timer, String tag) {
@@ -155,7 +154,7 @@
final Activity activity = getActivity();
// TODO just pass in a listener in newInstance()
if (activity instanceof AlarmLabelDialogHandler) {
- ((DeskClock) getActivity()).onDialogLabelSet(alarm, label, tag);
+ ((DeskClock) activity).onDialogLabelSet(alarm, label, tag);
} else {
LogUtils.e("Error! Activities that use LabelDialogFragment must implement "
+ "AlarmLabelDialogHandler");
@@ -167,7 +166,7 @@
final Activity activity = getActivity();
// TODO just pass in a listener in newInstance()
if (activity instanceof TimerLabelDialogHandler){
- ((DeskClock) getActivity()).onDialogLabelSet(timer, label, tag);
+ ((DeskClock) activity).onDialogLabelSet(timer, label, tag);
} else {
LogUtils.e("Error! Activities that use LabelDialogFragment must implement "
+ "AlarmLabelDialogHandler or TimerLabelDialogHandler");