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 */ 16package com.example.android.supportv7.app; 17 18import android.os.Bundle; 19import android.view.LayoutInflater; 20import android.view.Menu; 21import android.view.MenuInflater; 22import android.view.MenuItem; 23import android.view.View; 24import android.view.ViewGroup; 25import android.widget.Spinner; 26import android.widget.Toast; 27 28import androidx.annotation.Nullable; 29import androidx.appcompat.app.AppCompatActivity; 30import androidx.appcompat.app.AppCompatDialog; 31import androidx.appcompat.app.AppCompatDialogFragment; 32 33import com.example.android.supportv7.R; 34 35/** 36 * This demonstrates idiomatic usage of AppCompatDialogFragment. 37 */ 38public class DialogFragmentUsage extends AppCompatActivity { 39 40 private Spinner mSpinner; 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.dialog_usage); 46 47 mSpinner = findViewById(R.id.spinner_dialogs); 48 49 // Add an OnClickListener to show our selected dialog 50 findViewById(R.id.btn_show_dialog).setOnClickListener(new View.OnClickListener() { 51 @Override 52 public void onClick(View view) { 53 showSelectedDialog(); 54 } 55 }); 56 } 57 58 private void showSelectedDialog() { 59 switch (mSpinner.getSelectedItemPosition()) { 60 case 0: 61 showSimpleDialog(); 62 break; 63 case 1: 64 showButtonBarDialog(); 65 break; 66 } 67 } 68 69 private void showSimpleDialog() { 70 MenuDialogFragment fragment = MenuDialogFragment.create(R.layout.dialog_content); 71 fragment.show(getSupportFragmentManager(), null); 72 } 73 74 private void showButtonBarDialog() { 75 MenuDialogFragment fragment = MenuDialogFragment.create(R.layout.dialog_content_buttons); 76 fragment.show(getSupportFragmentManager(), null); 77 } 78 79 /** 80 * A simple {@link AppCompatDialog} implementation which 81 * inflates some items into it's options menu, and shows a toast when one is selected. 82 */ 83 public static class MenuDialogFragment extends AppCompatDialogFragment { 84 85 private static final String PARAM_CONTENT_VIEW = "content_view"; 86 87 static MenuDialogFragment create(int contentView) { 88 Bundle b = new Bundle(); 89 b.putInt(PARAM_CONTENT_VIEW, contentView); 90 91 MenuDialogFragment fragment = new MenuDialogFragment(); 92 fragment.setArguments(b); 93 94 return fragment; 95 } 96 97 @Override 98 public void onCreate(@Nullable Bundle savedInstanceState) { 99 super.onCreate(savedInstanceState); 100 setHasOptionsMenu(true); 101 } 102 103 @Nullable 104 @Override 105 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 106 @Nullable Bundle savedInstanceState) { 107 Bundle args = getArguments(); 108 int contentView = args.getInt(PARAM_CONTENT_VIEW); 109 return inflater.inflate(contentView, container, false); 110 } 111 112 @Override 113 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 114 inflater.inflate(R.menu.actions, menu); 115 } 116 117 @Override 118 public boolean onOptionsItemSelected(MenuItem item) { 119 Toast.makeText(getActivity(), "Dialog action selected: " + item.getTitle(), 120 Toast.LENGTH_SHORT).show(); 121 return true; 122 } 123 } 124 125} 126