MainActivity.xml
<?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:background="@color/colorPrimaryDark"
tools:context="com.example.bhaumik.sharedpreferences.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layouts">
<EditText
android:id="@+id/et_username"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="Enter UserName"
android:padding="10dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:background="#FFF"/>
<EditText
android:id="@+id/et_password"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:padding="10dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:background="#FFF"/>
</LinearLayout>
<Button
android:id="@+id/btn_save"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textAllCaps="false"
android:layout_marginLeft="30dp"
android:text="Save It"
android:layout_below="@+id/layouts"
android:textSize="17dp"/>
<Button
android:id="@+id/btn_display"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textAllCaps="false"
android:layout_marginLeft="200dp"
android:text="Display Info"
android:layout_below="@+id/layouts"
android:textSize="17dp"/>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_display"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:textColor="#FFF"/>
</RelativeLayout>
MainActivity.java
package com.example.bhaumik.sharedpreferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText username,password;
Button save,display;
TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.et_username);
password = (EditText) findViewById(R.id.et_password);
save = (Button) findViewById(R.id.btn_save);
display = (Button) findViewById(R.id.btn_display);
show = (TextView) findViewById(R.id.tv_show);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("username",username.getText().toString());
editor.putString("password",password.getText().toString());
editor.apply();
Toast.makeText(MainActivity.this,"User Saved...",Toast.LENGTH_SHORT).show();
}
});
display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
String names = preferences.getString("username","");
String pwd = preferences.getString("password","");
show.setText("UserName :-> " + names + " " + " Password :-> " + pwd);
}
});
}
}
0 comments:
Post a Comment