blob: bb7a50bd8750cf3a4a9d8bc8013adf683045aba7 [file] [log] [blame]
Hans Boehma3723842015-06-24 17:54:13 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.calculator2;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.content.Context;
24import android.os.Bundle;
25import android.view.LayoutInflater;
26import android.widget.TextView;
27
28public class AlertDialogFragment extends DialogFragment {
29
30 private static final String NAME = AlertDialogFragment.class.getName();
31 private static final String KEY_MESSAGE = NAME + "_message";
32 private static final String KEY_BUTTON_NEGATIVE = NAME + "_button_negative";
33
34 public static void showMessageDialog(Activity activity, CharSequence message) {
35 final Bundle args = new Bundle();
36 args.putCharSequence(KEY_MESSAGE, message);
37 args.putCharSequence(KEY_BUTTON_NEGATIVE, activity.getString(R.string.dismiss));
38
39 final AlertDialogFragment dialogFragment = new AlertDialogFragment();
40 dialogFragment.setArguments(args);
41 dialogFragment.show(activity.getFragmentManager(), null /* tag */);
42 }
43
44 public AlertDialogFragment() {
45 setStyle(STYLE_NO_TITLE, android.R.attr.alertDialogTheme);
46 }
47
48 @Override
49 public Dialog onCreateDialog(Bundle savedInstanceState) {
50 final Bundle args = getArguments() == null ? Bundle.EMPTY : getArguments();
51 final Context context = getContext();
52 final LayoutInflater inflater = LayoutInflater.from(context);
53 final TextView textView = (TextView) inflater.inflate(R.layout.dialog_message,
54 null /* root */);
55 textView.setText(args.getCharSequence(KEY_MESSAGE));
56 return new AlertDialog.Builder(context)
57 .setView(textView)
58 .setNegativeButton(args.getCharSequence(KEY_BUTTON_NEGATIVE), null /* listener */)
59 .create();
60 }
61}