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