Part 19 - Simple SharedPreferences

 Java Part : 

package first.learn.sharedpreference;

import static first.learn.sharedpreference.Common.getBooleanFromPref;
import static first.learn.sharedpreference.Common.getStringFromPref;
import static first.learn.sharedpreference.Common.saveBooleanToPref;
import static first.learn.sharedpreference.Common.saveStringToPref;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


public static final String NAME = "NAME" ;
public static final String ADDRESS = "ADDRESS" ;
public static final String AGE = "AGE" ;
public static final String MARITAL_STATUS = "MARITAL_STATUS" ;


private String name = "";
private String age = "";
private String address= "";
private boolean maritalStatus = false ;




EditText etAddress,
etAge,
etName;

CheckBox checkbox;
Button btnSave, btnShow;





Context mContext ;

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

mContext = this ;


etAddress = (EditText)findViewById(R.id.etAddress);
etAge = (EditText)findViewById(R.id.etAge);
etName = (EditText)findViewById(R.id.etName);
checkbox = (CheckBox) findViewById(R.id.checkbox);



btnSave = (Button) findViewById(R.id.btnSave);
btnShow = (Button) findViewById(R.id.btnShow);


btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if(!isEditTextHasText(etName)
|| !isEditTextHasText(etAge)
|| !isEditTextHasText(etAddress)){

Toast.makeText(MainActivity.this, "Please Fill All The Field", Toast.LENGTH_SHORT).show();
return;
}


name = etName.getText().toString();
age = etAge.getText().toString();
address = etAddress.getText().toString();

saveStringToPref(mContext, NAME,name );
saveStringToPref(mContext, ADDRESS,address );
saveStringToPref(mContext, AGE, age );



maritalStatus = checkbox.isChecked();
saveBooleanToPref(mContext, MARITAL_STATUS, maritalStatus );

Toast.makeText(mContext, "Saved SuccessFully", Toast.LENGTH_SHORT).show();


}
});


btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

name = getStringFromPref( mContext, NAME , "Not Saved");
etName.setText(name);

address = getStringFromPref(mContext, ADDRESS, "No Address");
etAddress.setText(address);

age = getStringFromPref(mContext, AGE, "No Age");
etAge.setText(age);

maritalStatus = getBooleanFromPref ( mContext, MARITAL_STATUS, false);

checkbox.setChecked(maritalStatus);


Toast.makeText(mContext, "Alhamdulillah, Data Retrieve Successfully", Toast.LENGTH_SHORT).show();



}
});







}

boolean isEditTextHasText (EditText editText){

String s = editText.getText().toString();

if(TextUtils.isEmpty(s)){
return false ;
}else {
return true ;
}

}




}


xml Part : 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="16dp">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="name....."
android:id="@+id/etName"/>


<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="age....."
android:id="@+id/etAge"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="address....."
android:id="@+id/etAddress"/>

<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Marital Status"
android:id="@+id/checkbox"/>


<Button
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSave"
android:text="Save"/>

<Button
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnShow"
android:text="Retrieve"/>


</LinearLayout>


Common Class : 


package first.learn.sharedpreference;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

public class Common {


public static void saveStringToPref(Context mContext, String packageName, String value ){

SharedPreferences settings = mContext.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();

editor.putString(packageName, value);
editor.apply();

}


public static void saveBooleanToPref(Context mContext, String packageName, boolean value ){

SharedPreferences settings = mContext.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();

editor.putBoolean(packageName, value);
editor.apply();

}


public static String getStringFromPref(Context mContext, String packetName, String defaultValue){

String s = defaultValue ;

SharedPreferences settings = mContext.getSharedPreferences("MyPref", Context.MODE_PRIVATE);


s = settings.getString(packetName, defaultValue);

return s ;


}

public static boolean getBooleanFromPref (Context mContext, String packageName, boolean value){

SharedPreferences settings = mContext.getSharedPreferences("MyPref", Context.MODE_PRIVATE);


boolean b = settings.getBoolean(packageName, value);

return b ;

}




}





No comments:

Post a Comment