1/* 2 * Copyright (C) 2014 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.app.Dialog; 19import android.content.Context; 20import android.os.Bundle; 21import android.support.v7.app.AppCompatActivity; 22import android.support.v7.app.AppCompatDialog; 23import android.view.Menu; 24import android.view.MenuItem; 25import android.view.View; 26import android.widget.Spinner; 27import android.widget.Toast; 28 29import com.example.android.supportv7.R; 30 31/** 32 * This demonstrates idiomatic usage of AppCompatDialog. 33 */ 34public class DialogUsage extends AppCompatActivity { 35 36 private Spinner mSpinner; 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.dialog_usage); 42 43 mSpinner = findViewById(R.id.spinner_dialogs); 44 45 // Add an OnClickListener to show our selected dialog 46 findViewById(R.id.btn_show_dialog).setOnClickListener(new View.OnClickListener() { 47 @Override 48 public void onClick(View view) { 49 showSelectedDialog(); 50 } 51 }); 52 } 53 54 private void showSelectedDialog() { 55 switch (mSpinner.getSelectedItemPosition()) { 56 case 0: 57 showSimpleDialog(); 58 break; 59 case 1: 60 showButtonBarDialog(); 61 break; 62 } 63 } 64 65 private void showSimpleDialog() { 66 Dialog dialog = new AppCompatDialog(this); 67 dialog.setTitle(R.string.dialog_title); 68 dialog.setContentView(R.layout.dialog_content); 69 dialog.show(); 70 } 71 72 private void showButtonBarDialog() { 73 Dialog dialog = new AppCompatDialog(this); 74 dialog.setTitle(R.string.dialog_title); 75 dialog.setContentView(R.layout.dialog_content_buttons); 76 dialog.show(); 77 } 78 79 /** 80 * A simple {@link android.support.v7.app.AppCompatDialog} implementation which 81 * inflates some items into it's options menu, and shows a toast when one is selected. 82 */ 83 private class MenuDialog extends AppCompatDialog { 84 85 public MenuDialog(Context context) { 86 super(context); 87 } 88 89 @Override 90 public boolean onCreateOptionsMenu(Menu menu) { 91 getMenuInflater().inflate(R.menu.actions, menu); 92 return true; 93 } 94 95 @Override 96 public boolean onOptionsItemSelected(MenuItem item) { 97 Toast.makeText(getOwnerActivity(), "Dialog action selected: " + item.getTitle(), 98 Toast.LENGTH_SHORT).show(); 99 return true; 100 } 101 } 102 103} 104