1 /*
2  * Copyright (C) 2014 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 package com.example.androidx.app;
17 
18 import android.os.Bundle;
19 import android.view.View;
20 
21 import androidx.appcompat.app.AlertDialog;
22 import androidx.appcompat.app.AppCompatActivity;
23 import androidx.appcompat.app.AppCompatDelegate;
24 
25 import com.example.androidx.R;
26 
27 import org.jspecify.annotations.Nullable;
28 
29 /**
30  * This demonstrates idiomatic usage of AlertDialog with Theme.AppCompat.DayNight
31  */
32 public class AppCompatNightModeAlertDialog extends AppCompatActivity {
33 
34     @Override
onCreate(@ullable Bundle savedInstanceState)35     protected void onCreate(@Nullable Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         setContentView(R.layout.appcompat_night_mode);
38     }
39 
setModeNightFollowSystem(View view)40     public void setModeNightFollowSystem(View view) {
41         AlertDialog dialog = createAlertDialog();
42         dialog.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
43         dialog.show();
44     }
45 
setModeNightNo(View view)46     public void setModeNightNo(View view) {
47         AlertDialog dialog = createAlertDialog();
48         dialog.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
49         dialog.show();
50     }
51 
setModeNightYes(View view)52     public void setModeNightYes(View view) {
53         AlertDialog dialog = createAlertDialog();
54         dialog.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
55         dialog.show();
56     }
57 
setModeNightAutoTime(View view)58     public void setModeNightAutoTime(View view) {
59         AlertDialog dialog = createAlertDialog();
60         dialog.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_TIME);
61         dialog.show();
62     }
63 
setModeNightAutoBattery(View view)64     public void setModeNightAutoBattery(View view) {
65         AlertDialog dialog = createAlertDialog();
66         dialog.getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
67         dialog.show();
68     }
69 
createAlertDialog()70     private AlertDialog createAlertDialog() {
71         AlertDialog.Builder b = new AlertDialog.Builder(this,
72                 androidx.appcompat.R.style.Theme_AppCompat_DayNight_Dialog_Alert);
73         b.setTitle(R.string.dialog_title);
74         b.setMessage(R.string.dialog_content);
75         return b.create();
76     }
77 
78 }
79