浏览代码

moving some setup from xml to code, adding button functionality
light mode button breaks my phone tho, just seems to go into an infinite loop

Tareef 4 年之前
父节点
当前提交
1970a01c1a

+ 23 - 5
app/src/main/java/com/tarfeef101/pwm/MainActivity.kt

@@ -1,26 +1,44 @@
 package com.tarfeef101.pwm
 
+import android.annotation.SuppressLint
 import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
 import android.widget.RadioButton
 import android.widget.RadioGroup
+import android.widget.TextClock
 import android.widget.TextView
+import androidx.appcompat.app.AppCompatDelegate
 import kotlinx.android.synthetic.main.activity_main.*
 
 class MainActivity : AppCompatActivity()
 {
+    @SuppressLint("SetTextI18n")
     override fun onCreate(savedInstanceState: Bundle?)
     {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
 
-        // set textview to radio group active option
-        fun setThemeStatus()
+        // define objects, set text values for em
+        themeRadioGroup.check(radioButtonDefault.id)
+        radioButtonDefault.setText("System Default")
+        radioButtonDark.setText("Dark")
+        radioButtonLight.setText("Light")
+
+        // set system theme based on active user choice
+        fun setTheme()
         {
-            var active = findViewById<RadioButton>(themeRadioGroup.checkedRadioButtonId)
+            val active = findViewById<RadioButton>(themeRadioGroup.checkedRadioButtonId)
+            when (active)
+            {
+                radioButtonDefault -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
+                radioButtonDark -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
+                radioButtonLight -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
+            }
             themeStatus.setText(active.getText())
         }
-        setThemeStatus()
-        themeRadioGroup.setOnCheckedChangeListener { _, _ -> setThemeStatus() }
+
+        // invoke the above on app startup, and set the listener to run from then on
+        setTheme()
+        themeRadioGroup.setOnCheckedChangeListener { _, _ -> setTheme()}
     }
 }

+ 6 - 11
app/src/main/res/layout/activity_main.xml

@@ -10,7 +10,6 @@
         android:id="@+id/themeRadioGroup"
         android:layout_width="156dp"
         android:layout_height="120dp"
-        android:checkedButton="@id/radioButton0"
         app:layout_constraintBottom_toTopOf="@+id/themeStatus"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintHorizontal_bias="0.5"
@@ -18,22 +17,19 @@
         app:layout_constraintTop_toTopOf="parent">
 
         <RadioButton
-            android:id="@+id/radioButton0"
+            android:id="@+id/radioButtonDefault"
             android:layout_width="match_parent"
-            android:layout_height="40dp"
-            android:text="@string/radio_default" />
+            android:layout_height="40dp" />
 
         <RadioButton
-            android:id="@+id/radioButton1"
+            android:id="@+id/radioButtonDark"
             android:layout_width="match_parent"
-            android:layout_height="40dp"
-            android:text="@string/radio_dark" />
+            android:layout_height="40dp" />
 
         <RadioButton
-            android:id="@+id/radioButton2"
+            android:id="@+id/radioButtonLight"
             android:layout_width="match_parent"
-            android:layout_height="40dp"
-            android:text="@string/radio_light" />
+            android:layout_height="40dp" />
     </RadioGroup>
 
     <TextView
@@ -41,7 +37,6 @@
         android:layout_width="91dp"
         android:layout_height="56dp"
         android:layout_marginBottom="16dp"
-        android:text="TextView"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintHorizontal_bias="0.5"

+ 0 - 3
app/src/main/res/values/strings.xml

@@ -1,6 +1,3 @@
 <resources>
     <string name="app_name">PWM</string>
-    <string name="radio_default">System Default</string>
-    <string name="radio_dark">Dark</string>
-    <string name="radio_light">Light</string>
 </resources>