Just remove the line toggle.toggle();
from your click listener toggle()
method will always reset your toggle button value.
And as you are trying to take the value of EditText
in string variable which always remains same as you are getting value in onCreate()
so better directly use the EditText
to get the value of it in your onClick
listener.
Just change your code as below its working fine now.
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//toggle.toggle();
if ( ed.getText().toString().equalsIgnoreCase("1")) {
toggle.setTextOff("TOGGLE ON");
toggle.setChecked(true);
} else if ( ed.getText().toString().equalsIgnoreCase("0")) {
toggle.setTextOn("TOGGLE OFF");
toggle.setChecked(false);
}
}
});