Home | History | Annotate | Download | only in com.example.android.permissionrequest
      1 /*
      2  * Copyright (C) 2016 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.permissionrequest;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.content.DialogInterface;
     22 import android.os.Bundle;
     23 import android.support.annotation.NonNull;
     24 import android.support.annotation.StringRes;
     25 import android.support.v4.app.DialogFragment;
     26 
     27 /**
     28  * Shows a dialog with a brief message.
     29  */
     30 public class MessageDialogFragment extends DialogFragment {
     31 
     32     private static final String ARG_MESSAGE_RES_ID = "message_res_id";
     33 
     34     public static MessageDialogFragment newInstance(@StringRes int message) {
     35         MessageDialogFragment fragment = new MessageDialogFragment();
     36         Bundle args = new Bundle();
     37         args.putInt(ARG_MESSAGE_RES_ID, message);
     38         fragment.setArguments(args);
     39         return fragment;
     40     }
     41 
     42     @NonNull
     43     @Override
     44     public Dialog onCreateDialog(Bundle savedInstanceState) {
     45         return new AlertDialog.Builder(getContext())
     46                 .setMessage(getArguments().getInt(ARG_MESSAGE_RES_ID))
     47                 .setCancelable(false)
     48                 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
     49                     @Override
     50                     public void onClick(DialogInterface dialog, int which) {
     51                         ((Listener) getParentFragment()).onOkClicked();
     52                     }
     53                 })
     54                 .create();
     55     }
     56 
     57     interface Listener {
     58         void onOkClicked();
     59     }
     60 
     61 }
     62