i'm working on custom radio button. it works fine(as it is defined in options.xml) but when i switch from options.xml to main.xml, it turns default, means it is no more highlighted. it should work like until i press it it should not turn to default.. here is radiobutton_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/radio_down" android:state_checked="true"/>
<item android:drawable="#drawable/radio" android:state_checked="false"/>
</selector>
i'm using these in options.xml to call radio button settings.
<RadioGroup
android:id="#+id/sound"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_gravity="right"
android:layout_marginRight="0dp"
android:layout_marginTop="50dp"
android:orientation="vertical"
android:padding="0dp" >
<RadioButton
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:button="#drawable/radiobutton_selector"
android:id="#+id/on"
/>
<RadioButton
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:button="#drawable/radiobutton_selector"
android:id="#+id/off"
/>
</RadioGroup>
Please help me to figure out the issue. Thanks in advance !!!!!
You can do like this for your RadioGroup.
you need to save which radio Button you have selected ,for that you can use one variable like below.
int check_radio=-1;
public static final int RADIO_BUTTON_ON=1;
public static final int RADIO_BUTTON_OFF=2;
mRadioGroup
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
switch(checkedId){
case R.id.on:
//Radio Button on is True
check_radio=RADIO_BUTTON_ON;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("RadioButton", RADIO_BUTTON_ON);
// Commit the edits!
editor.commit();
break;
case R.id.off:
//Radio Button off is True
check_radio=RADIO_BUTTON_OFF;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("RadioButton", RADIO_BUTTON_OFF);
// Commit the edits!
editor.commit();
break;
}
});
Now whey your Activity 's Resume you can check one condition like below.
Get Value from SharedPrefrence Like below code;
//If you have save your value in SharedPrefrence it will return your stored int value.
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
check_radio = settings.getInt("RadioButton", -1);
if(check_radio==RADIO_BUTTON_ON){
mRadioOn.setChecked(true);
}else if(check_radio==RADIO_BUTTON_OFF){
mRadioOff.setChecked(true);
}
you can use SharedPreferences by below step
public static final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
Related
I'm trying to make a Login/Registration activity by using sharedpreference. The app will only hold one login/password information.
So on original start up the app will check if the user has already made an account. This will be kept under a boolean called Registered. If that is true then it will enable the login button and disable the register button. If it is false then it will enable the register button and disable the login button.
When the types in his wanted username and password they can then hit the register button which will set the username and password into strings while changing the boolean to true and then put them in the sharedpreferences. Then the activity will refresh itself.
Currently i cannot get the buttons to change. I think i may be doing sharedpreferences wrong as this is my first time working with them. (Im new to android development.)
Here is my current code.
I have updated the code to include editor.apply()
I have added a few bits and pieces, mainly getting the value at start.
FINAL UPDATE: I have fixed the problem. It seemed to lie within the .setenabled i was using for the buttons. Once i switched them to .setvisibility, they switched perfectly. Thank you all for your help!
public class MainActivity extends AppCompatActivity {
EditText Username_input, Password_input;
String Username, Password;
//if false then no, if true then yes
boolean Registered;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Registered = sharedPref.getBoolean("Registered", false);
Button RegisterButton = (Button) findViewById(R.id.Register_btn);
Button Loginbutton = (Button) findViewById(R.id.Login_btn);
// If the user is registered.
if (Registered == false) {
Loginbutton.setEnabled(false);
RegisterButton.setEnabled(true);
// If the user is registered already.
} else {
Loginbutton.setEnabled(true);
RegisterButton.setEnabled(false);
}
RegisterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Username_input = (EditText) findViewById(R.id.Username_input);
Password_input = (EditText) findViewById(R.id.Password_input);
Username = Username_input.getText().toString();
Password = Password_input.getText().toString();
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("Registered", true);
editor.putString("Username", Username);
editor.putString("Password", Password);
editor.apply();
finish();
startActivity(getIntent());
}
});
}
}
XML Code
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/Username_txtview"
android:id="#+id/Username_txtview"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:editable="false" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/Username_input"
android:enabled="true"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:layout_below="#+id/Username_txtview"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/Password_txtview"
android:id="#+id/Password_txtview"
android:layout_below="#+id/Username_input"
android:layout_alignLeft="#+id/Username_input"
android:layout_alignStart="#+id/Username_input" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/Password_input"
android:enabled="true"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:layout_below="#+id/Password_txtview"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:text="#string/Login_btn"
android:id="#+id/Login_btn"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Register_btn"
android:id="#+id/Register_btn"
android:layout_below="#+id/Login_btn"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="26dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Registered = sharedPref.getBoolean("Registered", false);
if (!Registered) {
Loginbutton.setVisibility(View.GONE);
RegisterButton.setVisibility(View.VISIBLE);
// If the user is registered already.
} else{
Loginbutton.setVisibility(View.VISIBLE);
RegisterButton.setVisibility(View.GONE);
}
RegisterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Username_input = (EditText) findViewById(R.id.Username_input);
Password_input = (EditText) findViewById(R.id.Password_input);
Username = Username_input.getText().toString();
Password = Password_input.getText().toString();
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("Registered", true);
editor.putString("Username", Username);
editor.putString("Password", Password);
editor.apply();
finish();
startActivity(getIntent());
}
});
add editor.commit() after you put the value into preferences.
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("Registered", Registered);
editor.putString("Username", Username);
editor.putString("Password", Password);
editor.commit();
You are making two mistakes :
not fetching the registered value from shared preferences before checking it's value initially.
not doing editor.apply () or editor.commit() after putting new values.
try this (no need of taking shared pref as final)
SharedPreferences sharedPref = getContext().getSharedPreferences("hello", getContext().MODE_PRIVATE);
Boolean register = sharedPref.getBoolean("key",true);
and inside register.onClick
SharedPreferences sharedPref = getContext().getSharedPreferences("hello", getContext().MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPref.edit();
//your rest of the code to put string values into shared pref here.
editor.putBoolean("key",false);
editor.apply();
I'm new on android. I want to change the color of a text when a toggle button is checked and then save the state of both toggle button and text color even when the app is killed. can somebody give me some tips of how to do that. thanks.
here is simple example of toggle button and text view with Preferences(for save state).
create layout file below way
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton" />
</LinearLayout>
in your activity
public class MainActivity extends AppCompatActivity {
TextView textItem;
SharedPreferences sPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
// init Preferences
sPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
// init view
textItem = (TextView) findViewById(R.id.textView);
ToggleButton syncSwitch = (ToggleButton) findViewById(R.id.toggleButton1);
// toggle button event
syncSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set text color method
setTextColor(isChecked);
// save Toggle button state in Preference
SharedPreferences.Editor ed = sPref.edit();
ed.putBoolean("ToggleButton_CHECK", isChecked);
ed.commit();
}
});
// set default color if TextView called when activity started only for first time
boolean saveState = sPref.getBoolean("ToggleButton_CHECK", false);
setTextColor(saveState);
syncSwitch.setChecked(saveState);
}
public void setTextColor(boolean isChecked) {
if (isChecked) {
// button is on
textItem.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.agro_purple));
} else {
// button is off
textItem.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.colorAccent));
}
}
}
Hope it help!
I have an ImageView(see image below), which is clickable. When the user clicks on the ImageView, he gets redirected to website. This is all running fine.
My question is, how to let user know, he can click the image, how to make this feature visible?
I can add text under the image, but thats not, what i want. I would like some small icon or window(like dialog), which can be closed.
Thanks!
you can check by bellow:
imageViewObject.isClickable();
Maybe you could animate a small finger/hand icon tapping the image when the ImageView shows up to indicate the element is clickable.
Like this: http://cmsresources.windowsphone.com/windowsphone/en-us/How-to/wp7/block/gettingstarted-concept-tap-and-hold-gesture.png
Or you could use gradients and shadows to give the image a button look.
So I searched and found this. This is #kgandroid suggested.
I add a little bit of code:
private FrameLayout mFrame;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFrame = new FrameLayout(this);
mFrame.addView(LayoutInflater.from(getBaseContext()).inflate(R.layout.activity_single_list_view, null));
setContentView(mFrame);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean overlayShown = settings.getBoolean("overlayShown", false);
if(!overlayShown){
setupHelpOverlay();
}
...
}
private void setupHelpOverlay(){
final View tutView = LayoutInflater.from(getBaseContext()).inflate(R.layout.activity_detail_overlay, null);
if(mFrame != null){
mFrame.addView(tutView);
}
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("overlayShown", true);
// Commit the edits!
editor.commit();
Button btnDismiss = (Button) tutView.findViewById(R.id.button_overlay);
btnDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
mFrame.removeView(tutView);
}
});
}
And the activity_deatil_overlay.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent_gray" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/white"
android:text="#string/overlay_text"
android:paddingBottom="10dp" />
<View
android:id="#+id/textView1"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="#+id/button_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/overlay_button"
android:textColor="#color/white"
android:paddingTop="10dp" />
</RelativeLayout>
On the first start, it shows help overlay.
You could put a bar around the Image and change its color when the image is pressed. So it looks like a button.
I am trying to change the background colour of a activity where the user can change the colour using 3 radio buttons in an android app. I am doing this using sharedPreferences.
So I have the activity page where the colour is chosen with the radio group and the code to use sharedPrefernces looks like this (i know the switch statement works aswell as I have a toast message apperring when the colour is supposed to change):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
String colourSelected = "";
switch (checkedId) {
case R.id.radioButton1 :
colourSelected = "YELLOW";
editor.putString("colour", colourSelected);
editor.commit();
break;
case R.id.radioButton2 :
colourSelected = "YELLOW";
editor.putString("colour", colourSelected);
editor.commit();
break;
case R.id.radioButton3 :
colourSelected = "BLUE";
editor.putString("colour", colourSelected);
editor.commit();
break;
}
}
});
}
The XML looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.practical3_10327751_donnacha_holmes.Preferences" >
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radiogroup"
>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
Then there is the activity that changes the background colour, which looks like this when trying to change the colour:
public class Activity2 extends ActionBarActivity {
RelativeLayout rl;
String colour;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE);
colour = prefs.getString("Colour", "WHITE");
rl = (RelativeLayout)findViewById(R.id.RelativeLayout);
if(colour=="GREEN"){
rl.setBackgroundColor(Color.GREEN);
}
else if(colour=="YELLOW"){
rl.setBackgroundColor(Color.YELLOW);
}
else if(colour=="BLUE"){
rl.setBackgroundColor(Color.BLUE);
}
else{
rl.setBackgroundColor(Color.RED);
}
I know that the background colour is being changed because it is being set to red every time I go to this page.
Thanks for any help!
The == operator checks to see if the two strings are exactly the same object.
The .equals() method will check if the two strings have the same value.
Therefore to compare Strings use .equals(),i.e. rewrite your comparison as
if(colour.equals("GREEN")){
rl.setBackgroundColor(Color.GREEN);
}
else if(colour.equals("YELLOW")){
rl.setBackgroundColor(Color.YELLOW);
}
else if(colour.equals("BLUE")){
rl.setBackgroundColor(Color.BLUE);
}
else{
rl.setBackgroundColor(Color.RED);
}
and you are saving value as colour and try to retrieving as Colour,so change
colour = prefs.getString("Colour", "WHITE");
to
colour = prefs.getString("colour", "WHITE");
I have a class in which I have an option button that opens a dialog containing 6 checkboxes. By default, 4 checkboxes are set to true when the app is first loaded. The user can check and uncheck these checkboxes. When I press the backbutton, the dialog disappears and if again click the "option" button the checkboxes are not checked/unchecked as per the last state selected. I have used shared preferences to hold these values.
My Java class code is here. I have removed the unnecessary methods below and also posted the xml layout. Please help as to why the shared preferences is not working.
GlamDokuActivity
public class GlamDokuActivity extends Activity{
private Button easy,fair,hard,evil,insane;
private CheckBox screenOn,clashSquares,quickNotes,soundOn,showTimer,matchingNumbers;
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
public static final String DefaultChoicesOn = "defaults";
public static final String Timer ="true";
public static final String QuickNotes ="true";
public static final String ScreenOn ="false";
public static final String ClashingSquares ="true";
public static final String SoundOn ="false";
public static final String MatchingNumbers ="true";
//keeping a tab on the status of the above default options
private boolean timerStatus=false,quickNotesStatus=false,screenOnOffStatus=false,clashingSquaresStatus=false,soundOnOffStatus=false,matchingNumberStatus=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glamdoku_main);
//call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
sharedpreferences=getSharedPreferences(GlamDokuActivity.DefaultChoicesOn, Context.MODE_PRIVATE);
//saving in the shared preferences
editor = sharedpreferences.edit();
//If the key is not present then only add the key-value pair
if(!sharedpreferences.contains(Timer))editor.putBoolean(GlamDokuActivity.Timer, true);
if(!sharedpreferences.contains(ClashingSquares))editor.putBoolean(GlamDokuActivity.ClashingSquares, true);
if(!sharedpreferences.contains(MatchingNumbers))editor.putBoolean(GlamDokuActivity.MatchingNumbers, true);
if(!sharedpreferences.contains(SoundOn))editor.putBoolean(GlamDokuActivity.SoundOn, false);
if(!sharedpreferences.contains(ScreenOn))editor.putBoolean(GlamDokuActivity.ScreenOn, false);
if(!sharedpreferences.contains(QuickNotes))editor.putBoolean(GlamDokuActivity.QuickNotes, true);
editor.commit();
//KEEP THE SCREEN ON
if(sharedpreferences.getBoolean(ScreenOn, true))getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//System.out.println("I AM IN ONCREATE OF ");
}//onCreate() ends
public void optionsMethod(View v){//options button click method
//dialog that appears when the hints button is clicked
final Dialog dialog = new Dialog(GlamDokuActivity.this,R.style.dialogStyle);
dialog.setContentView(R.layout.layout_options);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(true);
dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
dialog.show();
//tagging the ids
screenOn = (CheckBox)dialog.findViewById(R.id.screenOn);
clashSquares = (CheckBox)dialog.findViewById(R.id.clash);
quickNotes = (CheckBox)dialog.findViewById(R.id.quickNotes);
soundOn = (CheckBox)dialog.findViewById(R.id.soundOnOff);
showTimer = (CheckBox)dialog.findViewById(R.id.timer);
matchingNumbers = (CheckBox)dialog.findViewById(R.id.matchNumbers);
//default values from shared preferences --- depending upon these the state of the respective checkboxes to change
timerStatus=sharedpreferences.getBoolean(GlamDokuActivity.Timer, true);
quickNotesStatus=sharedpreferences.getBoolean(GlamDokuActivity.QuickNotes, true);
screenOnOffStatus=sharedpreferences.getBoolean(GlamDokuActivity.ScreenOn, false);
clashingSquaresStatus=sharedpreferences.getBoolean(GlamDokuActivity.ClashingSquares, true);
soundOnOffStatus=sharedpreferences.getBoolean(GlamDokuActivity.SoundOn, false);
matchingNumberStatus=sharedpreferences.getBoolean(GlamDokuActivity.MatchingNumbers, true);
//check or uncheck depending upon the aforementioned boolean values
if(timerStatus==false)showTimer.setChecked(false); else showTimer.setChecked(true);
if(quickNotesStatus==false)quickNotes.setChecked(false);else quickNotes.setChecked(true);
if(screenOnOffStatus==false)screenOn.setChecked(false);else screenOn.setChecked(true);
if(clashingSquaresStatus==false)clashSquares.setChecked(false);else clashSquares.setChecked(true);
if(soundOnOffStatus==false)soundOn.setChecked(false);else soundOn.setChecked(true);
if(matchingNumberStatus==false)matchingNumbers.setChecked(false);else matchingNumbers.setChecked(true);
//-----------------------------------------------------------------------------------------------------------------
//click the screenOn chkbox
screenOn.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,1));
//click the clashing squares chkbox
clashSquares.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,2));
//click the quick notes chkbox
quickNotes.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,3));
//click the sound chkbox
soundOn.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,4));
//click the show timer chkbox
showTimer.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,5));
//click the matching numbers chkbox
matchingNumbers.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,6));
#Override
public void onResume(){
//KEEP THE SCREEN ON
if(sharedpreferences.getBoolean(ScreenOn, true))getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onResume();
}
}
CommonCheckClickListener
public class CommonCheckClickListener implements OnClickListener {
private Dialog dialog;
private SherlockActivity activity;
private int thisOption;
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
public CommonCheckClickListener(Dialog dialog, Activity activity, int thisOption){
this.activity=activity;
this.dialog=dialog;
this.thisOption=thisOption;
//call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
sharedpreferences=activity.getSharedPreferences(GlamDokuActivity.DefaultChoicesOn, Context.MODE_PRIVATE);
editor=sharedpreferences.edit();
}
#Override
public void onClick(View v) {
int flag=0;
if(((CheckBox)v).isChecked())flag=1; else flag=0;//flagged as checked
switch(thisOption){
case 1: //screenOn option
if(flag==1){
editor.putBoolean(GlamDokuActivity.ScreenOn, true);
Toast.makeText(activity, "Warning - Keeping the screen always on consumes battery.", Toast.LENGTH_LONG).show();
}
else{
editor.putBoolean(GlamDokuActivity.ScreenOn, false);
}
editor.commit();
boolean screenOn = sharedpreferences.getBoolean(GlamDokuActivity.ScreenOn, true);
break;
case 2: //clashing squares option
if(flag==1)editor.putBoolean(GlamDokuActivity.ClashingSquares, true);else editor.putBoolean(GlamDokuActivity.ClashingSquares, false);
editor.commit();
boolean cs = sharedpreferences.getBoolean(GlamDokuActivity.ClashingSquares, true);
break;
case 3: //quick notes option
if(flag==1)editor.putBoolean(GlamDokuActivity.QuickNotes, true); else editor.putBoolean(GlamDokuActivity.QuickNotes, false);
editor.commit();
boolean qn = sharedpreferences.getBoolean(GlamDokuActivity.QuickNotes, true);
break;
case 4: //sound option
if(flag==1)editor.putBoolean(GlamDokuActivity.SoundOn, true); else editor.putBoolean(GlamDokuActivity.SoundOn, false);
editor.commit();
boolean sound = sharedpreferences.getBoolean(GlamDokuActivity.SoundOn, true);
break;
case 5: //show Timer option
if(flag==1)editor.putBoolean(GlamDokuActivity.Timer, true); else editor.putBoolean(GlamDokuActivity.Timer, false);
editor.commit();
boolean timer = sharedpreferences.getBoolean(GlamDokuActivity.Timer, true);
break;
case 6: //matching numbers option
if(flag==1)editor.putBoolean(GlamDokuActivity.MatchingNumbers, true); else editor.putBoolean(GlamDokuActivity.MatchingNumbers, false);
editor.commit();
boolean matching = sharedpreferences.getBoolean(GlamDokuActivity.MatchingNumbers, true);
break;
}
}
Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
>
<Button
style="#style/button_main_screen"
android:id="#+id/playsudoku"
android:onClick="playMethod"
android:text="#string/playsudoku" />
<Button
style="#style/button_main_screen"
android:id="#+id/options"
android:onClick="optionsMethod"
android:text="#string/optionssudoku" />
</LinearLayout>
Dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical" >
<CheckBox
android:id="#+id/matchNumbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginTop="21dp"
android:text="#string/match_numbers" />
<CheckBox
android:id="#+id/clash"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/clash_squares" />
<CheckBox
android:checked="true"
android:id="#+id/quickNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/quick_notes" />
<CheckBox
android:id="#+id/timer"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/timer_always" />
<CheckBox
android:id="#+id/screenOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:text="#string/screen_always" />
<CheckBox
android:id="#+id/soundOnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="25dp"
android:text="#string/sound_off" />
</LinearLayout>
The definition of the static key-variables is incorrect. They need to be other names rather than "true" and "false". The problem is solved once you change the initialization values. – Ganesh Hegde
You should use OnCheckedChangeListener instead of OnClickListener for your CheckBoxes. OnClickListener#onClick calls earlier then CheckBox's checked state changed.