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