Home | History | Annotate | Download | only in calculator2
      1 /*
      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 
     17 package com.android.calculator2;
     18 
     19 import android.app.Activity;
     20 import android.app.AlertDialog;
     21 import android.app.Dialog;
     22 import android.app.DialogFragment;
     23 import android.content.Context;
     24 import android.os.Bundle;
     25 import android.view.LayoutInflater;
     26 import android.widget.TextView;
     27 
     28 public 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 }
     62