Question

I would like to set up a toggle button for my android app, but I have to keep the button state for later use. I was trying to do it using sharedPreferences, my values return fine form the pref file, but the text of my button is the same, it wont change.

I have tested by putting the: tb.setChecked(false); on the end of my on create which changes the text from "on" to "off". But I can't get the same effect via my code.

What could be wrong? Thanks!

main code:

public SharedPreferences spref;
final String PREF_NAME = "prefrences";
public String STATE = "state_value";
ToggleButton tb;
boolean on;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tour_detail);

     getActionBar().setDisplayHomeAsUpEnabled(true);
     getActionBar().setHomeButtonEnabled(true);

    Bundle b = getIntent().getExtras();
    tour = b.getParcelable(".Tour");
    isMyTours = b.getBoolean("isMyTours");
    isFollowed = b.getBoolean("isFollowed");

     Log.i(LOGTAG, "Extra passed by notif is: " + tour);

     refreshDisplay();      

     datasource = new ToursDataSource(this);

     spref = getSharedPreferences(PREF_NAME, MODE_PRIVATE);      
     tb = (ToggleButton) findViewById(R.id.buttonFollow);        

     on = spref.getBoolean("On", true);  //default is true       
     Log.i(LOGTAG, "VALUE for On is: " + on);        
     if (on = true) {            
       tb.setChecked(true);        
     } else {          
       tb.setChecked(false);
     }       
}   

public void onToggleClicked(View view) {

    on = ((ToggleButton) view).isChecked();
    if (on) {
        Toast.makeText(this, "On : Notification will be Enabled", Toast.LENGTH_SHORT).show();
        SharedPreferences.Editor editor = spref.edit();
        editor.putBoolean("On", true); // value to store
        editor.commit();
        boolean test_on = spref.getBoolean("On", true); 
        Log.i(LOGTAG, "VALUE for On is: " + test_on);

    } else {
    Toast.makeText(this, "Off : Notification will be Disabled", Toast.LENGTH_SHORT).show();
        SharedPreferences.Editor editor = spref.edit();
        editor.putBoolean("On", false); // value to store
        editor.commit();
        boolean test_on = spref.getBoolean("On", true); 
        Log.i(LOGTAG, "VALUE for On is: " + test_on);
    }  
}

and button inside XML:

 <ToggleButton
            android:id="@+id/buttonFollow"
            style="?android:attr/buttonStyleSmall"
            android:layout_weight="1"  
            android:layout_width="match_parent"           
            android:layout_height="wrap_content"              
            android:background="#dedede"   
            android:textSize="12sp"    
            android:paddingTop="7dp"  
            android:paddingBottom="5dp"  
            android:drawableTop="@drawable/urmarire_detail_ico"
            android:drawablePadding="-1sp"            
            android:textOn="Vibrate on"
            android:textOff="Vibrate off" 
            android:onClick="onToggleClicked"/> 
Was it helpful?

Solution

You have a bug in the condition check:

 if (on = true) {            
   tb.setChecked(true);        
 } else {          
   tb.setChecked(false);
 }

In Java, the comparison is done with == - in your code, value of on is set to true in the condition part of the if, so onCreate always sets the ToggleButton checked.

As a side note, it is a bad habit to compare a boolean to true - it is generally more readable code if you simply test if (on). However in your case this test is not even needed - you can simply replace the block with tb.setChecked(on)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top