summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-02-13 16:39:08 +0100
committerlonkaars <loek@pipeframe.xyz>2024-02-13 16:39:08 +0100
commitbf4decf024fa1bf9f3879c665c8fc2f5405ece4b (patch)
treec5e41f5fa4776308e259358f19cb91649bca170c
parentbce1be54a1661c8512fa5fe24ba0ec35c4a9ff96 (diff)
store button state in SharedPreferences
-rw-r--r--app/src/main/java/com/lonkaars/bingo3/MainActivity.java20
-rw-r--r--app/src/main/res/layout/activity_main.xml2
-rw-r--r--app/src/main/res/values/strings.xml2
3 files changed, 23 insertions, 1 deletions
diff --git a/app/src/main/java/com/lonkaars/bingo3/MainActivity.java b/app/src/main/java/com/lonkaars/bingo3/MainActivity.java
index 6440c3c..55d2424 100644
--- a/app/src/main/java/com/lonkaars/bingo3/MainActivity.java
+++ b/app/src/main/java/com/lonkaars/bingo3/MainActivity.java
@@ -2,15 +2,25 @@ package com.lonkaars.bingo3;
import androidx.appcompat.app.AppCompatActivity;
+import android.content.Context;
import android.content.Intent;
+import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
+import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
+ private SharedPreferences prefs;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
+
+ prefs = getSharedPreferences(getString(R.string.app_id), Context.MODE_PRIVATE);
+ View sw = findViewById(R.id.db_switch);
+ ((Switch) sw).setChecked(prefs.getBoolean(getString(R.string.pref_db_btn), false));
+ switch_handler(sw);
}
public void navigate_orientation(View view) {
@@ -22,4 +32,14 @@ public class MainActivity extends AppCompatActivity {
Intent intent = new Intent(this, WeatherActivity.class);
startActivity(intent);
}
+
+ public void switch_handler(View view) {
+ Switch sw = (Switch) view;
+ boolean state = sw.isChecked();
+ sw.setText(state ? "aan" : "uit");
+
+ SharedPreferences.Editor db = prefs.edit();
+ db.putBoolean(getString(R.string.pref_db_btn), state);
+ db.apply(); // this one is asynchronous
+ }
}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 30b029b..9a72a48 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -81,12 +81,12 @@
android:id="@+id/db_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:onClick="switch_handler"
android:rotation="-29"
android:rotationX="26"
android:rotationY="26"
android:scaleX="3"
android:scaleY="3"
- android:text="aan uit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 35af133..85c9c51 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1,4 +1,6 @@
<resources>
<string name="app_name">bingo3</string>
+ <string name="app_id">com.lonkaars.bingo3</string>
+ <string name="pref_db_btn">com.lonkaars.bingo3.db_button</string>
<string name="title_activity_orientation_demo">orientation_demo</string>
</resources> \ No newline at end of file