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