1 /* 2 * Copyright (C) 2019 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.android.car.themeplayground; 18 19 import android.app.AlertDialog; 20 import android.os.Bundle; 21 import android.widget.Button; 22 import android.widget.TextView; 23 import android.widget.Toast; 24 25 26 /** 27 * Activity that shows different dialogs from the device default theme. 28 */ 29 public class DialogSamples extends AbstractSampleActivity { 30 31 @Override onCreate(Bundle savedInstanceState)32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 Utils.onActivityCreateSetTheme(this); 35 setContentView(R.layout.dialog_samples); 36 37 Button showDialogButton = findViewById(R.id.showDialogBT); 38 Button showDialogOnlyPositiveButton = findViewById(R.id.showDialogOnlyPositiveBT); 39 Button showDialogWithoutTitleButton = findViewById(R.id.showDialogWithoutTitle); 40 Button showDialogWithCheckboxButton = findViewById(R.id.showDialogWithCheckboxBT); 41 setupBackgroundColorControls(R.id.dialogLayout); 42 showDialogButton.setOnClickListener(v -> openDialog(false)); 43 showDialogOnlyPositiveButton.setOnClickListener(v -> openDialogWithOnlyPositiveButton()); 44 showDialogWithoutTitleButton.setOnClickListener(v -> openDialogWithoutTitle()); 45 showDialogWithCheckboxButton.setOnClickListener(v -> openDialog(true)); 46 Button showToast = findViewById(R.id.showToast); 47 showToast.setOnClickListener(v -> showToast()); 48 } 49 50 openDialog(boolean showCheckbox)51 private void openDialog(boolean showCheckbox) { 52 53 AlertDialog.Builder builder = new AlertDialog.Builder(this, 54 R.style.Theme_Testing_Dialog_Alert); 55 56 if (showCheckbox) { 57 // Set Custom Title 58 TextView title = new TextView(builder.getContext()); 59 // Title Properties 60 title.setText("Custom Dialog Box"); 61 builder.setCustomTitle(title); 62 builder.setMultiChoiceItems(new CharSequence[]{"I am a checkbox"}, 63 new boolean[]{false}, 64 (dialog, which, isChecked) -> { 65 }); 66 } else { 67 builder.setTitle("Standard Alert Dialog") 68 .setMessage("With a message to show."); 69 } 70 71 builder.setPositiveButton("OK", (dialoginterface, i) -> { 72 }).setNegativeButton("CANCEL", 73 (dialog, which) -> { 74 }); 75 builder.show(); 76 } 77 openDialogWithOnlyPositiveButton()78 private void openDialogWithOnlyPositiveButton() { 79 AlertDialog.Builder builder = new AlertDialog.Builder(this, 80 R.style.Theme_Testing_Dialog_Alert); 81 builder.setTitle("Standard Alert Dialog") 82 .setMessage("With a message to show."); 83 builder.setPositiveButton("OK", (dialoginterface, i) -> { 84 }); 85 builder.show(); 86 } 87 openDialogWithoutTitle()88 private void openDialogWithoutTitle() { 89 AlertDialog.Builder builder = new AlertDialog.Builder(this, 90 R.style.Theme_Testing_Dialog_Alert); 91 builder.setMessage("I dont have a titile."); 92 builder.setPositiveButton("OK", (dialoginterface, i) -> { 93 }).setNegativeButton("CANCEL", 94 (dialog, which) -> { 95 }); 96 builder.show(); 97 } 98 showToast()99 private void showToast() { 100 Toast.makeText(this, "Toast message looks like this", 101 Toast.LENGTH_LONG).show(); 102 } 103 } 104