Simple EditText Alert dialog ran before main activity created - java

Trying to display simple edit text dialog, requesting a string be provided before the rest of my application starts. Currently im trying to make it so the APIKEY of my app is request first thing, then once entered its saved a shared preference and then the dialog will not display. The current code is being reused from a old project of mine. If anybody can help point me in the right direct to making this simple dialog.
public void getapikey() {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
LayoutInflater adbInflater = LayoutInflater.from(this);
View eulaLayout = adbInflater.inflate(R.layout.custom_dialog, null);
editText = (EditText) eulaLayout.findViewById(R.id.editText1);
adb.setView(eulaLayout);
adb.setTitle("Api Key");
adb.setMessage("Welcome to the app, Please input your APIkey below");
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
// CheckBox Confirm for Alert Dialog
public void onClick(DialogInterface dialog, int which) {
String value = editText.getText().toString();
if (editText !=null)
//Unsure about this part above and below
editText = "0"
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("apikey", value);
// Commit the edits!
editor.commit();
return;
}
});
// Preferences For Alert Dialog
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String apikey = settings.getString("apikey", "0");
if (apikey!=null )
adb.setIcon(R.drawable.ic_launcher);
adb.show();
}
}
RECOMMENDED CHANGES
public class Welcome extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
public EditText editText;
public String value;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getapikey();
}
public void getapikey() {
// Alert Dialog
AlertDialog.Builder adb = new AlertDialog.Builder(this);
LayoutInflater adbInflater = LayoutInflater.from(this);
View eulaLayout = adbInflater.inflate(R.layout.custom_dialog, null);
// dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.checkBox1);
editText = (EditText) eulaLayout.findViewById(R.id.editText1);
adb.setView(eulaLayout);
adb.setTitle("Api Key");
adb.setMessage("Welcome to the app, Please input your APIkey below");
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//String checkBoxResult = "NOT checked";
String value = editText.getText().toString();
// if (dontShowAgain.isChecked())
// checkBoxResult = "checked";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
//editor.putString("skipMessage", checkBoxResult);
editor.putString("apikey", value);
// Commit the edits!
editor.commit();
return;
}
});
// Preferences For Alert Dialog
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
//String skipMessage = settings.getString("skipMessage", "NOT checked");
String apikey = settings.getString("apikey", value);
if(!value.equals(""))
adb.setIcon(R.drawable.ic_launcher);
adb.show();
setContentView(R.layout.splash_screen);
Thread splashThread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
// changed from 5000 to 4000 11.29
while (waited < 3000) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
// do nothing
} finally {
Intent i = new Intent();
i.setClassName("com.example.app",
"com.example.app.CardsTesting");
startActivity(i);
finish();
}
}
};
splashThread.start();
}
}
Still doesnt save the preference after the first time then never displays

//try this one i think it may be work
SharedPreferences settings = PreferenceManager.getSharedPreferences(PREFS_NAME, 0);

Related

how disable button on click when the application first time runs

In my application I have 2 buttons.
The first button in start game and the second button in continue game.
I want to disable my second button for the first time when my application runs.
It means just for the first time my second button is disabled to onClick. How to handle this?
How can I make it understand it is the first time?
public class Menu extends Activity implements View.OnClickListener {
SharedPreferences prefs;
Editor edit;
TextView best;
private boolean flag;
public static ImageView btn1, conti, but3, but4;
static Noti_Queue noti_queue;
static Splash splash;
public static AppList applist;
public static int userId;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.home_activity);
flag = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("flag", false);
if (applist == null) {
applist = new AppList(this);
applist.set_identity("1");
}
if (splash == null) {
splash = new Splash(this);
splash.set_identity("1");
}
if (noti_queue == null) {
noti_queue = new Noti_Queue(this);
noti_queue.set_identity("1");
}
btn1 = (ImageView) findViewById(R.id.button1);
conti = (ImageView) findViewById(R.id.btn2);
but3 = (ImageView) findViewById(R.id.button3);
but4 = (ImageView) findViewById(R.id.button4);
btn1.setOnClickListener(this);
conti.setOnClickListener(this);
conti.setBackgroundResource(R.drawable.cont);
if (!flag) {
flag = true;
conti.setBackgroundResource(R.drawable.cont_press);}
conti.setEnabled(flag);
but3.setOnClickListener(this);
but4.setOnClickListener(this);
// giving question id
final int que_id = getIntent().getIntExtra("integer", 0);
Log.e("mhs", que_id + "");
//now lets save the que_id(now it is save to SharedPreferences
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
userId = myPrefs.getInt("userId", 0);
//let get it to show
Log.e("saved", que_id + "");
setsize();
best = (TextView) findViewById(R.id.textView1);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
best.setTypeface(tf, Typeface.BOLD);
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
edit = prefs.edit();
if (prefs.getInt("Best", 0) <= 0) {
edit.putInt("Best", 0);
edit.commit();
}
best.setText("بیشترین امتیاز : " + prefs.getInt("Best", 0));
}
#Override
public void onBackPressed() {
splash.Display();
splash = null;
super.onBackPressed();
}
#Override
protected void onResume() {
best.setText("بیشترین امتیاز : " + prefs.getInt("Best", 0));
super.onResume();
}
private void setsize() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int h = dm.heightPixels;
int w = dm.widthPixels;
h = h / 6;
w = w - ((w * 30) / 100);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(w, h);
but4.setLayoutParams(params);
but3.setLayoutParams(params);
btn1.setLayoutParams(params);
conti.setLayoutParams(params);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
if (!flag) {
flag = true;
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("flag", flag).commit();
conti.setEnabled(flag);
conti.setBackgroundResource(R.drawable.cont_press);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
//finishing this activity is important to exit from app
Menu.this.finish();
}
break;
case R.id.btn2:
Toast.makeText(getApplicationContext(), userId + "", Toast.LENGTH_LONG).show();
Intent intent1 = new Intent(Menu.this, ContinueActivity.class);
intent1.putExtra("integer2", userId);
startActivity(intent1);
Menu.this.finish();
break;
case R.id.button3:
Toast.makeText(getApplicationContext(), "help", Toast.LENGTH_LONG).show();
break;
case R.id.button4:
applist.Display();
break;
}
}
With the help of sharedprefrence you can achieve this for example.
SharedPreferences sharedPreferences = getSharedPreferences("myPref", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("enableButton", true);
editor.commit();
put this code for enabling Button.
SharedPreferences sharedPreferences = getSharedPreferences("myPref", MODE_PRIVATE);
flag = sharedPreferences.getBoolean("enableButton", false);
if (flag) {
conti.setEnabled(true); ;
}

How to change background colour of an Android activity

I want to change the background colour of an activity using AlertDialog. Here is what I have done:
private SharedPreferences prefs;
private static final String SELECTED_ITEM = "SelectedItem";
private Editor sharedPrefEditor;
btnchangeColor = (ImageButton) findViewById(R.id.btnchangeColor);
btnchangeColor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CharSequence[] items={"Red","Green","Blue", "Yellow", "#EE82EE"};
AlertDialog.Builder builder = new AlertDialog.Builder(
ContentView_2.this);
builder.setTitle("Pick a Color");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {}
});
builder.setSingleChoiceItems(items, getSelectedItem(), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
wvContent = (WebView) findViewById(R.id.wvContent);
//LinearLayout ll=(LinearLayout)findViewById(R.id.wvContent);
if("Red".equals(items[which]))
{
wvContent.setBackgroundColor(Color.RED);
}
else if("Green".equals(items[which]))
{
wvContent.setBackgroundColor(Color.GREEN);
}
else if("Blue".equals(items[which]))
{
wvContent.setBackgroundColor(Color.BLUE);
}
else if("Yellow".equals(items[which]))
{
wvContent.setBackgroundColor(Color.YELLOW);
}
else if("#EE82EE".equals(items[which]))
{
wvContent.setBackgroundColor(Color.rgb(184,184,184));
}
saveSelectedItem(which);
}
});
builder.show();
}});
}
private int getSelectedItem() {
if (prefs == null) {
prefs = PreferenceManager
.getDefaultSharedPreferences(this);
}
return prefs.getInt(SELECTED_ITEM, -1);
}
private void saveSelectedItem(int which) {
if (prefs == null) {
prefs = PreferenceManager
.getDefaultSharedPreferences(this);
}
sharedPrefEditor = prefs.edit();
sharedPrefEditor.putInt(SELECTED_ITEM, which);
sharedPrefEditor.commit();
}
I also add this code to OnCreate:
wvContent = (WebView) findViewById(R.id.wvContent);
wvContent.setBackgroundColor(getSelectedItem());
The colour is selected and the web view (wvContent) changes to chosen colour, BUT this is not saved to and/or loaded from Shared Preferences. It returns to its default colour when the activity is relaunched.
So the question is: How can properly I save the selected colour to SharedPreferences and load it when activity relaunches?
Many thanks for help.
Currently you are saving position of selected item instead of color code in SharedPreferences. do it as:
wvContent = (WebView) findViewById(R.id.wvContent);
int bg_color=Color.TRANSPARENT;
if("Red".equals(items[which]))
{
wvContent.setBackgroundColor(Color.RED);
bg_color=Color.RED;
}
else if("Green".equals(items[which]))
{
wvContent.setBackgroundColor(Color.GREEN);
bg_color=Color.GREEN;
}
........
// save color in SharedPreferences
saveSelectedItem(bg_color);
Now getSelectedItem() method return color code for previously selected value from spinner.

My code seems to be sound but any time I click the submit button it does nothing

Alright I am comparing the text that the user has input to the edittext and comparing to the answer that is there in the String.xml. The code seems completely sounds and error-less but it doesn't work at all. Any help would be appreciated.
package com.example.italianapp;
public class Pictures<sButton> extends MainActivity
{
int level=0; // integer that will keep track of users progress
Button sButton; // represents the submit button
EditText mEdit; // text area where the user types answer
private SensorManager mSensorManager; // new sensor
private ShakeEventListener mSensorListener; // variable that listens for movement
// if the back button is pressed, return to easy menu
public void onBackPressed()
{
Intent backIntent = new Intent(Pictures.this, Learningmenu.class);
startActivity(backIntent);
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.words); // uses easy_mode1 xml
// shared preferences variable that will act as a holder of users progress
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
AlertDialog.Builder hint = new AlertDialog.Builder(this); // hint alert dialog box
hint.setMessage("Would you like a hint?");
hint.setCancelable(false);
hint.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
// if user clicks yes, toast pops up providing a hint
public void onClick(DialogInterface dialog, int id)
{
Toast.makeText(Pictures.this, "You have two of these on your body", Toast.LENGTH_LONG).show();
}
});
hint.setNegativeButton("No", new DialogInterface.OnClickListener()
{
// if the user clicks no the level is loaded again
public void onClick(DialogInterface dialog, int id)
{
Intent intent = new Intent(Pictures.this, Pictures.class);
startActivity(intent);
dialog.cancel();
}
});
final AlertDialog hintAlert = hint.create();
hintAlert.setTitle("Hint");
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorListener = new ShakeEventListener();
mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener()
{
// when the user shakes the device, alert them if they want a hint
public void onShake()
{
hintAlert.show();
}
});
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
TextView plot; // text view which will hold the plot
plot = (TextView)findViewById(R.id.question1); // look for text called question1 in xml
plot.setMovementMethod(new ScrollingMovementMethod());
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); // alert dialog box
alt_bld.setMessage("Correct! Proceed to next level?");
alt_bld.setCancelable(false);
alt_bld.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
// if user decides to continue to the next level, load the second easy level
public void onClick(DialogInterface dialog, int id)
{
Intent intent = new Intent(Pictures.this, Pictures.class);
startActivity(intent);
}
});
alt_bld.setNegativeButton("No", new DialogInterface.OnClickListener()
{
// if the user does not want to continue, load the easy mode levels
public void onClick(DialogInterface dialog, int id)
{
Intent intent = new Intent(Pictures.this, Pictures.class);
startActivity(intent);
dialog.cancel();
}
});
final AlertDialog alert = alt_bld.create();
alert.setTitle("Yes! Mano means Hand");
sButton = (Button)findViewById(R.id.ansSubmit1); // submit button will find ansSubmit button in xml
sButton.setOnClickListener(new View.OnClickListener()
{
// if the submit button is clicked check text entered to actual answer
public void onClick(View view)
{
mEdit = (EditText)findViewById(R.id.ansField1);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
Context context = getApplicationContext();
String text = getResources().getString(R.string.answer1);
// if the text that was entered matches the answer, alert dialog box will appear
if(mEdit.getText().toString().replaceAll(" ", "").replaceAll("-", "").toLowerCase().equals(text))
{
int level = app_preferences.getInt("level", 0);
if (level > 0)
{
level=level-1;
}
level++;
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("level", level);
editor.commit();
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.correct);
mediaPlayer.start();
alert.show();
}
else
{
// if the text that was entered does not match the answer, toast pop up = "Incorrect"
CharSequence message = "Incorrect!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, message, duration);
toast.show(); // show toast alert
v.vibrate(250); // vibrate device for 250ms
}
}
});
}
#Override
protected void onResume()
{
super.onResume();
mSensorManager.registerListener(mSensorListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
}
#Override
protected void onPause()
{
mSensorManager.unregisterListener(mSensorListener);
super.onStop();
}
}
public class Pictures<sButton> extends MainActivity
{
int level=0; // integer that will keep track of users progress
Button sButton; // represents the submit button
It appears your class is a Generic of type sButton - and sButton isn't a type.

Keep me logged in CheckBox Android

I am creating an android app for which I have created login and signup pages.
I want to implement the "keep me logged in" functionality in the login page.
I think I know what to do but almost anything I try is giving an error.
I have to store the state of checkbox in a SharedPreference and then use it with an if else statement, if it is checked it will skip the login activity and start the main activity, otherwise the login activity.
It would be helpful if someone could provide a sample code.
ChechBox keeplog = (CheckBox)findBiewById(R.Id.checkbox1)
How can I store the check state in Shared Preference and how to fetch its state in the form of boolean or string so that it can be used with if-else statement?
Try the following way, declare one boolean variable.
CheckBox keeplog = (CheckBox) findViewById(R.id.checkBox1);
boolean isChecked = false;
Then store the state of checkbox in shared preferences with CheckedChangeListener.
keeplog.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("isChecked", isChecked);
editor.commit();
}
});
Then now get the state of checkbox from SharedPreferences using following way.
SharedPreferences settings1 = getSharedPreferences("PREFS_NAME", 0);
isChecked = settings1.getBoolean("isChecked", false);
Now check with the if statement and start your activity like the following
if (isChecked) {
Intent i = new Intent(MainActivity.this, ThirdActivity.class);
startActivity(i);
} else {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
Thank you.
Try the following, I have pasted the code about how to connect & login to ejabberd server as without it the code was looking incomplete for Remember Me functionality. I have added comments for explaining how to store & retrieve values in SharedPrefernces.
public class Login extends Activity
{
private XMPPConnection connection;
private ProgressBar progressBar;
private EditText userId;
private EditText password;
private CheckBox rememberMe;
private SharedPreferences sharedPreferences;
private Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userId = (EditText) findViewById(R.id.user_id);
password = (EditText) findViewById(R.id.password);
rememberMe = (CheckBox) findViewById(R.id.remember_me);
progressBar = (ProgressBar) findViewById(R.id.login_progress);
//Retrieving SharedPreferences
sharedPreferences = this.getPreferences(Context.MODE_PRIVATE);
//Retrieving editor
editor = sharedPreferences.edit();
//Retrieving remember me checkbox's set value which if was stored in SharedPreferences
if(sharedPreferences.getBoolean(getString(R.string.remember_me), false))
{
String uId, pass;
uId = sharedPreferences.getString(getString(R.string.user_id), "");
pass = sharedPreferences.getString(getString(R.string.password), "");
userId.setText(uId);
password.setText(pass);
rememberMe.setChecked(sharedPreferences.getBoolean(getString(R.string.remember_me), false));
new Connection().execute(uId, pass);
}
}
public void login(View view)
{
new Connection().execute(userId.getText().toString(), password.getText().toString());
}
private class Connection extends AsyncTask<String, Void, Integer>
{
private static final int CONNECTION_FAILURE = 0;
private static final int LOGIN_FAILURE = 1;
private static final int SUCCESS = 2;
protected void onPreExecute()
{
progressBar.setVisibility(ProgressBar.VISIBLE);
}
protected Integer doInBackground(String... strings)
{
ConnectionConfiguration conConfig = new ConnectionConfiguration("192.168.1.66", 5222, "domain");
connection = new XMPPConnection(conConfig);
try
{
connection.connect();
Log.i("TESTING", "CONNECTED TO " + connection.getHost());
}
catch(Exception e)
{
Log.e("TESTING", e.getMessage());
return CONNECTION_FAILURE;
}
try
{
connection.login(strings[0], strings[1]);
Log.i("TESTING", "LOGGED IN AS " + connection.getUser());
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
}
catch(Exception e)
{
Log.e("TESTING", e.getMessage());
return LOGIN_FAILURE;
}
return SUCCESS;
}
protected void onPostExecute(Integer integer)
{
progressBar.setVisibility(ProgressBar.GONE);
switch(integer)
{
case CONNECTION_FAILURE:
Toast.makeText(getApplicationContext(), "Failed to connect to the server.", Toast.LENGTH_LONG).show();
break;
case LOGIN_FAILURE:
Toast.makeText(getApplicationContext(), "Please check your user id and or password.", Toast.LENGTH_LONG).show();
break;
case SUCCESS:
if(rememberMe.isChecked())
{
//Setting value in SharedPrefernces using editor.
editor.putBoolean(getString(R.string.remember_me), rememberMe.isChecked());
editor.putString(getString(R.string.user_id), userId.getText().toString());
editor.putString(getString(R.string.password), password.getText().toString());
//Committing the changes.
editor.commit();
}
else
//Clearing SharedPreferences.
sharedPreferences.edit().clear().commit();
startActivity(new Intent(getApplicationContext(), Home.class));
}
}
}
}

shared preference concept login page from server validation in android

i am validation of login page is done by server side.i create one checkbox for remember username and password.if check box is checked it remembers the username and password.But the problem is even if check box unchecked also it finds stored username and password. now i want if checkbox is unchecked means it cannot remember username and password
i try this code
public class Login extends Activity {
SharedPreferences app_preferences ;
CheckBox check;
private static final String UPDATE_URL = "serverurl";
public ProgressDialog progressDialog;
private EditText UserEditText;
private EditText PassEditText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait...");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
UserEditText = (EditText) findViewById(R.id.username);
PassEditText = (EditText) findViewById(R.id.password);
check=(CheckBox) findViewById(R.id.checkbox);
String Str_user = app_preferences.getString("username","0" );
String Str_pass = app_preferences.getString("password", "0");
String Str_check = app_preferences.getString("checked", "no");
if(Str_check.equals("yes"))
{
UserEditText.setText(Str_user);
PassEditText.setText(Str_pass);
check.setChecked(true);
}
Button button = (Button) findViewById(R.id.okbutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int usersize = UserEditText.getText().length();
int passsize = PassEditText.getText().length();
if(usersize > 0 && passsize > 0) {
progressDialog.show();
String user = UserEditText.getText().toString();
String pass = PassEditText.getText().toString();
String Str_check2 = app_preferences.getString("checked", "no");
if(Str_check2.equals("yes"))
{
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("username", user);
editor.putString("password", pass);
editor.commit();
}
doLogin(user, pass);
} else createDialog("Error","Please enter Username and Password");
}
});
check box
check.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Perform action on clicks, depending on whether it's now checked
SharedPreferences.Editor editor = app_preferences.edit();
if (((CheckBox) v).isChecked())
{
editor.putString("checked", "yes");
editor.commit();
}
else
{
editor.putString("checked", "no");
editor.commit();
}
}
});
try
String Str_check = app_preferences.getString("checked", "no");
if(Str_check.equals("yes"))
{
String Str_user = app_preferences.getString("username","0" );
String Str_pass = app_preferences.getString("password", "0");
UserEditText.setText(Str_user);
PassEditText.setText(Str_pass);
check.setChecked(true);
}
else{
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("username", "");
editor.putString("password", "");
editor.commit();
}
When the user is unchecking the checkbox, you should remove the stored details. Here you are keeping the value stored even the user has unchecked the checkbox.
check box
check.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Perform action on clicks, depending on whether it's now checked
SharedPreferences.Editor editor = app_preferences.edit();
if (((CheckBox) v).isChecked())
{
editor.putString("username", user);
editor.putString("password", pass);
editor.putString("checked", "yes");
editor.commit();
}
else
{
editor.putString("username", "");
editor.putString("password", "");
editor.putString("checked", "no");
editor.commit();
}
}
});

Categories

Resources