CodeDocu.com

App settings as app preferences save and read
 
Android Java preferences are used as internal data memory to save settings and read.
The settings are normally with settings pages specially adjusted and saved.
 
But also the preferences can be used as persistent data storage of variables, so the values are still retained even after switching off the app.
A good example here is the password and user field in an app.
You will save password and user not in a settings page, but directly to a login page.
So none as Settings Acitivty designed app page will be needed.
 
Instead, it will store the values directly into the login in the app store as preferences and read in one other place again at run time from the preferences.
App settings as app preferences save and read
 
 
Android preferences data structure
Android preferences are like an internal database tables.
When specifying the table reading, you can read table values from this preferences and write
App settings as app preferences save and read
 
Setting the preferences table
If an Android app values of internal preferences would like to read or write, then is in the initialize the SharedPreferences specified which internal table it is.
On an internal table with the name "Settings" is shown in the following code.
As soon as you write values into this table, Android creates the internal table if this was not already present.

settings=context.getSharedPreferences("settings", MODE_PRIVATE);
 

 
 
Save preferences
It uses a SharedPrefernces Editor to store.
In the initialize the SharedPreferences is at the the table defines.
With putString() you can store a value in the preferences table.

//< save >
SharedPreferences.Editor edit = settings.edit();
edit.clear();
edit.putString(
"password", sSetting);
edit.commit();
//</ save >


 

 
 
Reading preferences
To read values you can easily use the the method getString(key).

//----< read setting >----
String sSetting= settings.getString("password","Error");
//----</ read setting >----
 

 
 
 
Video tutorial for the direct settings for password
< iframe width = "854" height = "480" src = "https://www.youtube.com/embed/MjkzKdQahZk" frameborder = "0" allowfullscreen > < / iframe >
 
 
Complete MainActivity.java code

package de.rue25.a2016_10_07_1740_password_setting;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

//< Variables >
Activity context = this;
EditText
editText_Passwort;
SharedPreferences
settings ;
//</ Variables >

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main);
//< init >
editText_Passwort=(EditText) findViewById(R.id.editText_Password);
settings=context.getSharedPreferences("settings", MODE_PRIVATE);
//</ init >
}

public void btnSave_onClick(View view) {
//------------< btnSave_onClick() >------------
//< init >
String sSetting=editText_Passwort.getText().toString();
//</ init >

//< save >
SharedPreferences.Editor edit = settings.edit();
edit.clear();
edit.putString(
"password", sSetting);
edit.commit();
//</ save >
//------------</ btnSave_onClick() >------------
}

public void btnRead_onClick(View view) {
//------------< btnRead_onClick() >------------
//----< read setting >----
String sSetting= settings.getString("password","Error");
//----</ read setting >----

//< show as message >
Toast.makeText(this,sSetting,Toast.LENGTH_LONG).show();
//</ show as message >

//------------</ btnRead_onClick() >------------
}
}

 

 
MainActivity.xml
App settings as app preferences save and read
 
Activity_main.XML complete with test button and the password input field

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.rue25.a2016_10_07_1740_password_setting.MainActivity">

<
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password Setting"
android:textSize="24sp"/>

<
TextView
android:text="Password:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="80dp"
android:id="@+id/lblPassword"
android:textSize="18sp"/>

<
EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_alignTop="@+id/lblPassword"
android:layout_toRightOf="@+id/lblPassword"
android:layout_toEndOf="@+id/lblPassword"
android:id="@+id/editText_Password"
android:textSize="18sp"/>

<
Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText_Password"
android:layout_alignLeft="@+id/editText_Password"
android:layout_alignStart="@+id/editText_Password"
android:layout_marginTop="33dp"
android:id="@+id/button"
android:onClick="btnSave_onClick"/>

<
Button
android:text="Read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button"
android:layout_alignRight="@+id/editText_Password"
android:layout_alignEnd="@+id/editText_Password"
android:layout_marginRight="17dp"
android:layout_marginEnd="17dp"
android:id="@+id/btnRead"
android:onClick="btnRead_onClick"/>
</
RelativeLayout>
 

 
 
 

Software Entwicklung Stuttgart NĂ¼rtingen
Suche Projekte C#, WPF, Windows App,ASP.Net, vb.Net, WinForms, SQL Server, Access, Excel