I created an App which has a 4 digit PIN Number to Login. I saved that in a String varaible like String defaultPin="1234";
. I gave an option in OptionsMenu
to change PIN. If we click Change PIN it will prompt a Dialog
with three EditText
boxes to get Old PIN, New PIN and Re-Enter PIN and a Button
to save it. It works fine PIN changes. But if I restart the App again the new PIN is not working. Default PIN works.
My Code is
public class LoginActivity extends ActionBarActivity {
String defaultPin="1234";
EditText etOldPin,etNewPin,etRePin;
Button btnCPin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText etPin=(EditText)findViewById(R.id.editText);
final ImageButton btnTest=(ImageButton)findViewById(R.id.imageButton);
btnTest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (etPin.getText().toString().equals(defaultPin)){
Intent i=new Intent(LoginActivity.this,DashboardActivity.class);
startActivity(i);
} else {
Toast.makeText(LoginActivity.this,"Error! Check PIN",Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.changePin:
Toast.makeText(LoginActivity.this,"Change Pin Clicked",Toast.LENGTH_LONG).show();
showDialogPin();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void showDialogPin() {
AlertDialog.Builder alertBuilder=new AlertDialog.Builder(this);
LayoutInflater inflater=this.getLayoutInflater();
View dialogView=inflater.inflate(R.layout.change_pin, null);
alertBuilder.setView(dialogView);
AlertDialog alertDialog=alertBuilder.create();
alertDialog.show();
etOldPin=(EditText)alertDialog.findViewById(R.id.editText3);
etNewPin=(EditText)alertDialog.findViewById(R.id.editText9);
etRePin=(EditText)alertDialog.findViewById(R.id.editText10);
btnCPin=(Button)alertDialog.findViewById(R.id.button);
btnCPin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String strOldPin=etOldPin.getText().toString();
String strNewPin=etNewPin.getText().toString();
String strRePin=etRePin.getText().toString();
if (strOldPin.equals("") || strNewPin.equals("") || strRePin.equals("")) {
Toast.makeText(LoginActivity.this, "All Fields must be filled", Toast.LENGTH_LONG).show();
} else {
if (strOldPin.equals(defaultPin)) {
if (strNewPin.equals(strRePin)) {
defaultPin = strNewPin;
Toast.makeText(LoginActivity.this, "PIN Changed", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LoginActivity.this, "PIN's Mismatch", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(LoginActivity.this, "Please Enter exact Old PIN", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
This is my code. Screenshot is
Please Help me...
Edited with SharedPreference
public class LoginActivity extends ActionBarActivity {
EditText etOldPin,etNewPin,etRePin;
Button btnCPin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText etPin=(EditText)findViewById(R.id.editText);
final ImageButton btnTest=(ImageButton)findViewById(R.id.imageButton);
final SharedPreferences preferences=getSharedPreferences("account",MODE_PRIVATE);
Editor editor=preferences.edit();
editor.putString("pin","1234");
editor.commit();
editor.clear();
btnTest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (etPin.getText().toString().equals(preferences.getString("pin",""))) {
Intent i = new Intent(LoginActivity.this, DashboardActivity.class);
startActivity(i);
} else {
Toast.makeText(LoginActivity.this, "Error! Check PIN", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.changePin:
Toast.makeText(LoginActivity.this,"Change Pin Clicked",Toast.LENGTH_LONG).show();
showDialogPin();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void showDialogPin() {
AlertDialog.Builder alertBuilder=new AlertDialog.Builder(this);
LayoutInflater inflater=this.getLayoutInflater();
View dialogView=inflater.inflate(R.layout.change_pin, null);
alertBuilder.setView(dialogView);
AlertDialog alertDialog=alertBuilder.create();
alertDialog.setCancelable(true);
alertDialog.show();
etOldPin=(EditText)alertDialog.findViewById(R.id.editText3);
etNewPin=(EditText)alertDialog.findViewById(R.id.editText9);
etRePin=(EditText)alertDialog.findViewById(R.id.editText10);
btnCPin=(Button)alertDialog.findViewById(R.id.button);
btnCPin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String strOldPin=etOldPin.getText().toString();
String strNewPin=etNewPin.getText().toString();
String strRePin=etRePin.getText().toString();
SharedPreferences preferences=getSharedPreferences("account",MODE_PRIVATE);
Editor editor=preferences.edit();
String storedPin=preferences.getString("pin","");
if (strOldPin.equals("") || strNewPin.equals("") || strRePin.equals("")) {
Toast.makeText(LoginActivity.this, "All Fields must be filled", Toast.LENGTH_LONG).show();
} else {
if (strOldPin.equals(storedPin)) {
if (strNewPin.equals(strRePin)) {
editor.putString("pin",strNewPin);
editor.commit();
editor.clear();
Toast.makeText(LoginActivity.this, "PIN Changed", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LoginActivity.this, "PIN's Mismatch", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(LoginActivity.this, "Please Enter exact Old PIN", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
The reason it do not work after restarting app is that Shared preference is not updated with new pin.
You need to update the shared preference with the new pin in
btnCPin click listener
For updating shared preference look into
http://developer.android.com/training/basics/data-storage/shared-preferences.html