Skip to content

Instantly share code, notes, and snippets.

@BetelGeuseee
Created May 24, 2020 03:37
Show Gist options
  • Save BetelGeuseee/06789f0c3694a7e1576fa72973e096e3 to your computer and use it in GitHub Desktop.
Save BetelGeuseee/06789f0c3694a7e1576fa72973e096e3 to your computer and use it in GitHub Desktop.
Shared Preferences in Android
//There are two Activity ..First one takes the username and password from the user and saves in a XML file named "MyData"
// The next Activity loads the username and password in the textview in that activity
//ACTIVTY ONEEEEEEEEEEEEEEEEEEEEEE
package com.example.sharedpreferenceproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
Button save,next;
EditText user,pass;
TextView userview,passview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences=getSharedPreferences("MyData",MODE_PRIVATE);
editor = preferences.edit();
userview = (TextView)findViewById(R.id.userView);
passview=(TextView)findViewById(R.id.passView);
save=(Button)findViewById(R.id.saveButton);
next=(Button)findViewById(R.id.nextButton);
user=(EditText)findViewById(R.id.editText1);
pass=(EditText)findViewById(R.id.editText2);
}
public void save(View v){
editor.putString("name",user.getText().toString());
editor.putString("password",pass.getText().toString());
editor.commit();
}
public void next(View v){
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
}
}
//ACTIVTY TWOOOOOOOOOOOOOOOOOOOOOO
package com.example.sharedpreferenceproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
public static final String DEFAULT="N/A";
SharedPreferences preferences;
TextView user,pass;
Button load,prev;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
user = (TextView)findViewById(R.id.userView2);
pass = (TextView)findViewById(R.id.passView2);
preferences = getSharedPreferences("MyData",MODE_PRIVATE);
}
public void load(View v){
String name = preferences.getString("name",DEFAULT);
String password = preferences.getString("password",DEFAULT);
if(name.equals(DEFAULT) || password.equals(DEFAULT)){
Toast.makeText(this,"No data was Found",Toast.LENGTH_SHORT).show();
}
else{
user.setText(name);
pass.setText(password);
}
}
public void prev(View v){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment