Thursday, 22 August 2013

How to use Shared Preferences to make a simple App in Android (updated with androidx)

The simplest way to store data in an app would be only Shared Preferences.
We are going to make an a small and simple app which store data and no need to use any button to save it. Once you close the scree, whatever you have edited, remains as it is.


----------------------------------------- shared_preferences_activity.xml -----------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#F57F17"
android:gravity="center|top">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="8"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#E91E63"
android:background="#FFB300"
android:text="Five Goals for this Year"/>

<EditText
android:id="@+id/spa_goal1ET"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"/>

<EditText
android:id="@+id/spa_goal2ET"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"/>

<EditText
android:id="@+id/spa_goal3ET"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textStyle="bold"
android:textColor="#ffffff"
android:gravity="center"
android:textSize="20sp"/>

<EditText
android:id="@+id/spa_goal4ET"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"/>

<EditText
android:id="@+id/spa_goal5ET"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFB300"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#E91E63"
android:text="Progress in %"/>

<EditText
android:id="@+id/spa_progressET"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="0%"/>

</LinearLayout>
</LinearLayout>
</LinearLayout>
---------------------------------------------------------------
--------------------------------------- SharedPreferencesActivity.java ------------------------------------------
package com.app.yourappname;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class SharedPreferencesActivity extends AppCompatActivity {

private GameDB gdb;
private EditText goal1ET, goal2ET, goal3ET, goal4ET, goal5ET, progressET;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shared_preferences_activity);

gdb = new GameDB();

goal1ET = findViewById(R.id.spa_goal1ET);
goal2ET = findViewById(R.id.spa_goal2ET);
goal3ET = findViewById(R.id.spa_goal3ET);
goal4ET = findViewById(R.id.spa_goal4ET);
goal5ET = findViewById(R.id.spa_goal5ET);
progressET = findViewById(R.id.spa_progressET);

goal1ET.setText(gdb.getString("goal1"));
goal2ET.setText(gdb.getString("goal2"));
goal3ET.setText(gdb.getString("goal3"));
goal4ET.setText(gdb.getString("goal4"));
goal5ET.setText(gdb.getString("goal5"));
progressET.setText(""+gdb.getFloat("progress"));
}

public class GameDB {
private static final String PREF_NAME = "DATA";

public void setString(String key, String value) {
SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();
editor.putString(key, value);
editor.apply();
}

public String getString(String key) {
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
return prefs.getString(key,"");
}

public void setFloat(String key, float value) {
SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();
editor.putFloat(key, value);
editor.apply();
}

public float getFloat(String key) {
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
return prefs.getFloat(key,0.0f);
}
}

@Override
protected void onPause() {
super.onPause();
gdb.setString("goal1", goal1ET.getText().toString());
gdb.setString("goal2", goal2ET.getText().toString());
gdb.setString("goal3", goal3ET.getText().toString());
gdb.setString("goal4", goal4ET.getText().toString());
gdb.setString("goal5", goal5ET.getText().toString());
gdb.setFloat("progress", Float.parseFloat(progressET.getText().toString()));
}
}
---------------------------------------------------------------

------------------------------------------ Declare Activity name in Manifest file ---------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SharedPreferencesActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
---------------------------------------------------------------

No comments:

Post a Comment