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 17 package com.example.androidx.app; 18 19 import android.os.Bundle; 20 import android.view.View; 21 import android.widget.Spinner; 22 23 import androidx.appcompat.app.AlertDialog; 24 import androidx.appcompat.app.AppCompatActivity; 25 import androidx.appcompat.content.res.AppCompatResources; 26 27 import com.example.androidx.Cheeses; 28 import com.example.androidx.R; 29 30 /** 31 * This demonstrates idiomatic usage of AppCompat's AlertDialog. 32 */ 33 public class AlertDialogUsage extends AppCompatActivity { 34 35 private Spinner mSpinner; 36 37 @Override onCreate(Bundle savedInstanceState)38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.alert_dialog_usage); 41 42 mSpinner = findViewById(R.id.spinner_dialogs); 43 44 // Add an OnClickListener to show our selected dialog 45 findViewById(R.id.btn_show_dialog).setOnClickListener(new View.OnClickListener() { 46 @Override 47 public void onClick(View view) { 48 showSelectedDialog(); 49 } 50 }); 51 } 52 showSelectedDialog()53 private void showSelectedDialog() { 54 switch (mSpinner.getSelectedItemPosition()) { 55 case 0: 56 showSimpleDialog(); 57 break; 58 case 1: 59 showSimpleButtonsDialog(); 60 break; 61 case 2: 62 showSimpleWithMoreButtonsDialog(); 63 break; 64 case 3: 65 showSimpleWithStackedButtonsDialog(); 66 break; 67 case 4: 68 showSingleChoiceDialog(); 69 break; 70 case 5: 71 showMultiChoiceDialog(); 72 break; 73 } 74 } 75 showSimpleDialog()76 private void showSimpleDialog() { 77 AlertDialog.Builder b = new AlertDialog.Builder(this); 78 b.setTitle(R.string.dialog_title); 79 b.setMessage(R.string.dialog_content); 80 b.show(); 81 } 82 showSimpleButtonsDialog()83 private void showSimpleButtonsDialog() { 84 AlertDialog.Builder b = new AlertDialog.Builder(this); 85 b.setTitle(R.string.dialog_title); 86 b.setMessage(R.string.dialog_content); 87 b.setNegativeButton("-ve", null); 88 b.setPositiveButton("+ve", null); 89 b.show(); 90 } 91 showSimpleWithMoreButtonsDialog()92 private void showSimpleWithMoreButtonsDialog() { 93 AlertDialog.Builder b = new AlertDialog.Builder(this); 94 b.setTitle(R.string.dialog_title); 95 b.setMessage(R.string.dialog_content); 96 b.setNegativeButton("-ve", null); 97 b.setNegativeButtonIcon(AppCompatResources.getDrawable(this, R.drawable.ic_media_pause)); 98 b.setNeutralButton("=ve", null); 99 b.setNeutralButtonIcon(AppCompatResources.getDrawable(this, R.drawable.ic_media_play)); 100 b.setPositiveButton("+ve", null); 101 b.setPositiveButtonIcon(AppCompatResources.getDrawable(this, R.drawable.ic_media_stop)); 102 b.show(); 103 } 104 showSimpleWithStackedButtonsDialog()105 private void showSimpleWithStackedButtonsDialog() { 106 AlertDialog.Builder b = new AlertDialog.Builder(this); 107 b.setTitle(R.string.dialog_title); 108 b.setMessage(R.string.dialog_content); 109 b.setNegativeButton("Very long negative", null); 110 b.setNeutralButton("Very long neutral", null); 111 b.setPositiveButton("Very long positive", null); 112 b.show(); 113 } 114 showSingleChoiceDialog()115 private void showSingleChoiceDialog() { 116 AlertDialog.Builder b = new AlertDialog.Builder(this); 117 b.setTitle(R.string.dialog_title); 118 b.setSingleChoiceItems(Cheeses.sCheeseStrings, 0, null); 119 b.setPositiveButton("OK", null); 120 b.show(); 121 } 122 showMultiChoiceDialog()123 private void showMultiChoiceDialog() { 124 AlertDialog.Builder b = new AlertDialog.Builder(this); 125 b.setTitle(R.string.dialog_title); 126 b.setMultiChoiceItems(Cheeses.sCheeseStrings, null, null); 127 b.setPositiveButton("OK", null); 128 b.show(); 129 } 130 131 132 } 133