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 android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.content.DialogInterface;
     22 import android.os.Bundle;
     23 import android.util.Log;
     24 import android.view.View;
     25 import android.view.View.OnClickListener;
     26 import android.widget.Button;
     27 import android.widget.TextView;
     28 
     29 import androidx.fragment.app.DialogFragment;
     30 import androidx.fragment.app.FragmentActivity;
     31 
     32 import com.example.android.supportv4.R;
     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             @Override
     51             public void onClick(View v) {
     52                 showDialog();
     53             }
     54         });
     55     }
     56 
     57 //BEGIN_INCLUDE(activity)
     58     void showDialog() {
     59         DialogFragment newFragment = MyAlertDialogFragment.newInstance(
     60                 R.string.alert_dialog_two_buttons_title);
     61         newFragment.show(getSupportFragmentManager(), "dialog");
     62     }
     63 
     64     public void doPositiveClick() {
     65         // Do stuff here.
     66         Log.i("FragmentAlertDialog", "Positive click!");
     67     }
     68 
     69     public void doNegativeClick() {
     70         // Do stuff here.
     71         Log.i("FragmentAlertDialog", "Negative click!");
     72     }
     73 //END_INCLUDE(activity)
     74 
     75 //BEGIN_INCLUDE(dialog)
     76     public static class MyAlertDialogFragment extends DialogFragment {
     77 
     78         public static MyAlertDialogFragment newInstance(int title) {
     79             MyAlertDialogFragment frag = new MyAlertDialogFragment();
     80             Bundle args = new Bundle();
     81             args.putInt("title", title);
     82             frag.setArguments(args);
     83             return frag;
     84         }
     85 
     86         @Override
     87         public Dialog onCreateDialog(Bundle savedInstanceState) {
     88             int title = getArguments().getInt("title");
     89 
     90             return new AlertDialog.Builder(getActivity())
     91                     .setIcon(R.drawable.alert_dialog_icon)
     92                     .setTitle(title)
     93                     .setPositiveButton(R.string.alert_dialog_ok,
     94                         new DialogInterface.OnClickListener() {
     95                             @Override
     96                             public void onClick(DialogInterface dialog, int whichButton) {
     97                                 ((FragmentAlertDialogSupport)getActivity()).doPositiveClick();
     98                             }
     99                         }
    100                     )
    101                     .setNegativeButton(R.string.alert_dialog_cancel,
    102                         new DialogInterface.OnClickListener() {
    103                             @Override
    104                             public void onClick(DialogInterface dialog, int whichButton) {
    105                                 ((FragmentAlertDialogSupport)getActivity()).doNegativeClick();
    106                             }
    107                         }
    108                     )
    109                     .create();
    110         }
    111     }
    112 //END_INCLUDE(dialog)
    113 }
    114