1 /*
2  * Copyright (C) 2010 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.example.android.apis.app;
18 
19 import com.example.android.apis.R;
20 
21 import android.app.Activity;
22 import android.app.AlertDialog;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.content.DialogInterface;
26 import android.os.Bundle;
27 import android.util.Log;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.widget.Button;
31 import android.widget.TextView;
32 
33 /**
34  * Demonstrates how to show an AlertDialog that is managed by a Fragment.
35  */
36 public class FragmentAlertDialog extends Activity {
37 
38     @Override
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.fragment_dialog);
42 
43         View tv = findViewById(R.id.text);
44         ((TextView)tv).setText("Example of displaying an alert dialog with a DialogFragment");
45 
46         // Watch for button clicks.
47         Button button = (Button)findViewById(R.id.show);
48         button.setOnClickListener(new OnClickListener() {
49             public void onClick(View v) {
50                 showDialog();
51             }
52         });
53     }
54 
55 //BEGIN_INCLUDE(activity)
showDialog()56     void showDialog() {
57         DialogFragment newFragment = MyAlertDialogFragment.newInstance(
58                 R.string.alert_dialog_two_buttons_title);
59         newFragment.show(getFragmentManager(), "dialog");
60     }
61 
doPositiveClick()62     public void doPositiveClick() {
63         // Do stuff here.
64         Log.i("FragmentAlertDialog", "Positive click!");
65     }
66 
doNegativeClick()67     public void doNegativeClick() {
68         // Do stuff here.
69         Log.i("FragmentAlertDialog", "Negative click!");
70     }
71 //END_INCLUDE(activity)
72 
73 //BEGIN_INCLUDE(dialog)
74     public static class MyAlertDialogFragment extends DialogFragment {
75 
newInstance(int title)76         public static MyAlertDialogFragment newInstance(int title) {
77             MyAlertDialogFragment frag = new MyAlertDialogFragment();
78             Bundle args = new Bundle();
79             args.putInt("title", title);
80             frag.setArguments(args);
81             return frag;
82         }
83 
84         @Override
onCreateDialog(Bundle savedInstanceState)85         public Dialog onCreateDialog(Bundle savedInstanceState) {
86             int title = getArguments().getInt("title");
87 
88             return new AlertDialog.Builder(getActivity())
89                     .setIcon(R.drawable.alert_dialog_icon)
90                     .setTitle(title)
91                     .setPositiveButton(R.string.alert_dialog_ok,
92                         new DialogInterface.OnClickListener() {
93                             public void onClick(DialogInterface dialog, int whichButton) {
94                                 ((FragmentAlertDialog)getActivity()).doPositiveClick();
95                             }
96                         }
97                     )
98                     .setNegativeButton(R.string.alert_dialog_cancel,
99                         new DialogInterface.OnClickListener() {
100                             public void onClick(DialogInterface dialog, int whichButton) {
101                                 ((FragmentAlertDialog)getActivity()).doNegativeClick();
102                             }
103                         }
104                     )
105                     .create();
106         }
107     }
108 //END_INCLUDE(dialog)
109 }
110