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