From bf4decf024fa1bf9f3879c665c8fc2f5405ece4b Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 13 Feb 2024 16:39:08 +0100 Subject: store button state in SharedPreferences --- .../main/java/com/lonkaars/bingo3/MainActivity.java | 20 ++++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/values/strings.xml | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) 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 @@ bingo3 + com.lonkaars.bingo3 + com.lonkaars.bingo3.db_button orientation_demo \ No newline at end of file -- cgit v1.2.3