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 */ 16 17package com.example.android.supportv7.app; 18 19import com.example.android.supportv7.Cheeses; 20import com.example.android.supportv7.R; 21 22import android.os.Bundle; 23import android.support.v7.app.AlertDialog; 24import android.support.v7.app.AppCompatActivity; 25import android.view.View; 26import android.widget.Spinner; 27 28/** 29 * This demonstrates idiomatic usage of AppCompat's AlertDialog. 30 */ 31public class AlertDialogUsage extends AppCompatActivity { 32 33 private Spinner mSpinner; 34 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 setContentView(R.layout.alert_dialog_usage); 39 40 mSpinner = findViewById(R.id.spinner_dialogs); 41 42 // Add an OnClickListener to show our selected dialog 43 findViewById(R.id.btn_show_dialog).setOnClickListener(new View.OnClickListener() { 44 @Override 45 public void onClick(View view) { 46 showSelectedDialog(); 47 } 48 }); 49 } 50 51 private void showSelectedDialog() { 52 switch (mSpinner.getSelectedItemPosition()) { 53 case 0: 54 showSimpleDialog(); 55 break; 56 case 1: 57 showSimpleButtonsDialog(); 58 break; 59 case 2: 60 showSingleChoiceDialog(); 61 break; 62 case 3: 63 showMultiChoiceDialog(); 64 break; 65 } 66 } 67 68 private void showSimpleDialog() { 69 AlertDialog.Builder b = new AlertDialog.Builder(this); 70 b.setTitle(R.string.dialog_title); 71 b.setMessage(R.string.dialog_content); 72 b.show(); 73 } 74 75 private void showSimpleButtonsDialog() { 76 AlertDialog.Builder b = new AlertDialog.Builder(this); 77 b.setTitle(R.string.dialog_title); 78 b.setMessage(R.string.dialog_content); 79 b.setNegativeButton("-ve", null); 80 b.setPositiveButton("+ve", null); 81 b.show(); 82 } 83 84 private void showSingleChoiceDialog() { 85 AlertDialog.Builder b = new AlertDialog.Builder(this); 86 b.setTitle(R.string.dialog_title); 87 b.setSingleChoiceItems(Cheeses.sCheeseStrings, 0, null); 88 b.setPositiveButton("OK", null); 89 b.show(); 90 } 91 92 private void showMultiChoiceDialog() { 93 AlertDialog.Builder b = new AlertDialog.Builder(this); 94 b.setTitle(R.string.dialog_title); 95 b.setMultiChoiceItems(Cheeses.sCheeseStrings, null, null); 96 b.setPositiveButton("OK", null); 97 b.show(); 98 } 99 100 101} 102