Checking toggle button state in android? - android

How to maintain the state of toggle buttons, like if we press button1 then it remembers the on state of button1 and won't allow us to press button2, and vice versa.
Please help me out in solving this problem.

Try this.
Declare variables
ToggleButton mToggleOne;
ToggleButton mToggleTwo;
In onCreate
mToggleOne = (ToggleButton) findViewById(R.id.toggle_button1);
mToggleTwo = (ToggleButton) findViewById(R.id.toggle_button2);
SharedPreferences spref = getSharedPreferences();
if(spref.getBoolean("one", false)) {
mToggleOne.setChecked(true);
mToggleTwo.setEnabled(false);
} else if(spref.getBoolean("two", false)) {
mToggleTwo.setChecked(true);
mToggleOne.setEnabled(false);
}
mToggleOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mToggleTwo.setEnabled(!isChecked);
getSharedPreferences().edit()
.putBoolean("one", isChecked)
.apply();
//other code
}
});
mToggleTwo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mToggleOne.setEnabled(!isChecked);
getSharedPreferences().edit()
.putBoolean("two", isChecked)
.apply();
//other code
}
});

Related

how to get focus when checked chekbox?

I have set setFocusable(false) in my edit text. But when i checked checkbox i want to get focus in my edittext. below is example.
cbIsGap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
edittext1.setFocusable(true);
edittext1.setClickable(true);
}else {
edittext1.setFocusable(false);
edittext1.setClickable(false);
}
}
});
I am trying above example but i am not getting focus when i checked checkbox

Sharedpreferences with switch button

I have a project, i want to save state of switch button on sharedpreferences. but it not working. Switch button always off when i switch activity or close app and reopen. what wrong in my code ? It working on first test, when i rebuid app again it wrong,
public class huongdan extends Activity implements CompoundButton.OnCheckedChangeListener{
Switch vt,mbf,vnp,vnm,gmb;
TextView vtmb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_huongdan);
// hien thi man hinh float
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
getWindow().setLayout((int) (width*1),(int) (height*0.80));
// endhien thi man hinh float
//switch viettel
vt = (Switch) findViewById(R.id.switchvt);
vt.setChecked(getFromSP("sw1"));
vt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
OneSignal.sendTag("viettel", "viettel");
}
else
OneSignal.deleteTag("viettel");
}
});
//switch mobi
mbf = (Switch) findViewById(R.id.switchmbf);
mbf.setChecked(getFromSP("sw2"));
mbf.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
OneSignal.sendTag("mobifone", "mobifone");
}
else
OneSignal.deleteTag("mobifone");
}
});
//switch vina
vnp = (Switch) findViewById(R.id.switchvnp);
vnp.setChecked(getFromSP("sw3"));
vnp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
OneSignal.sendTag("vinaphone", "vinaphone");
}
else
OneSignal.deleteTag("vinaphone");
}
});
//switch vietnammobile
vnm = (Switch) findViewById(R.id.switchvnm);
vnm.setChecked(getFromSP("sw4"));
vnm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
OneSignal.sendTag("vietnammobile", "vietnammobile");
}
else
OneSignal.deleteTag("vietnammobile");
}
});
//switch gmobile
gmb = (Switch) findViewById(R.id.switchgmb);
gmb.setChecked(getFromSP("sw5"));
gmb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
OneSignal.sendTag("gmobile", "gmobile");
}
else
OneSignal.deleteTag("gmobile");
}
});
}
private boolean getFromSP(String key){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.sopu89.napthenoti", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.sopu89.napthenoti", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.switchvt:
saveInSp("sw1",isChecked);
break;
case R.id.switchmbf:
saveInSp("sw2",isChecked);
break;
case R.id.switchvnp:
saveInSp("sw3",isChecked);
break;
case R.id.switchvnm:
saveInSp("sw4",isChecked);
break;
case R.id.switchgmb:
saveInSp("sw5",isChecked);
break;
}
}
}
You need to retrieve the values from shared preferences and then set the button state in onCreate method
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean tgpref = preferences.getBoolean("tgpref", true); //default is true
if (tgpref = true) //if (tgpref) may be enough, not sure
{
tg.setChecked(true);
}
else
{
tg.setChecked(false);
}
the onCheckedChanged-method in the end will never be called and therefore your states are not going to be saved.
The reason for this is, that you are declaring a new listener for every switch.
call
saveinSp("sw1", isChecked);
in the listener of switch one or set your class as listener for the switches and handle all changes there:
switch.setOnCheckedChangedListener(this);

Overriding setOnCheckedChangeListener in Android

I have a set of switch components, around 30. I want to override the setOnCheckedChangeListener so that I can avoid unnecessary code that does nearly the same thing.
I know, it is possible for onClick function, so using multiple buttons and changing the logic in the overridden function is way better.
What I have so far is:
final Switch question1 = (Switch) findViewById(R.id.question1);
question1.setText(R.string.no);
question1.setChecked(false);
question1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
question1.setText(R.string.yes);
} else {
question1.setText(R.string.no);
}
UpdateScores();
}
});
final Switch question2 = (Switch) findViewById(R.id.question2);
question2.setText(R.string.no);
question2.setChecked(false);
question2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
question2.setText(R.string.yes);
} else {
question2.setText(R.string.no);
}
UpdateScores();
}
});
As can be seen much of the code is replicated and I don't want that. Is there any way to achive this?
You can create one listener for all your switchers.
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
buttonView.setText(R.string.yes);
} else {
buttonView.setText(R.string.no);
}
UpdateScores();
}
And then add this listener to your Switchers:
question1.setOnCheckedChangeListener(listener);
question2.setOnCheckedChangeListener(listener);
....
question30.setOnCheckedChangeListener(listener);
OnCheckedChangeListner onchange = new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
buttonView.setText(R.string.yes);
} else {
buttonView.setText(R.string.no);
}
UpdateScores();
}
}
question2.setOnCheckedChangeListener(onchange);
you want it?

Check if Any Checkbox is Checked

I have checkboxes for week days in my android application , and I want to put a listener to check if any one of these checkboxes is checked but my way seems hard , isn't there a way to gather all these listeners into one
sun.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
mon.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
tue.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
wed.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
thu.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
fri.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
sat.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
You can check the id of the buttonView that is being triggered and do the proper validation, all you have to do is assign the same onCheckListener to the checkboxes and do something as show below:
private OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.checkId1:
//TODO: Code for checked one...
break;
case R.id.checkId2:
//TODO: Code for checked two...
break;
}
}
};
Hope it Helps!
Regards!
If you have similar logic that you haven't provided here, you can create a list of checkboxes and create listeners for them in a cycle, for example.
Let your activity implement the OnCheckedChangeListener and then you have the onCheckChanged...
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.sun:
break;
case R.id.mon:
break;
default:
break;
}
}
there is; implements View.OnClickListener with his method in your class
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.checbox1:
if(checkbo1.isCheck){
//do logic then dont forget to set to the opposite state
checkbox.setChecked(false);
}
else{
//do logic
checkbox.setChecked(true);
}
break;
//
case R.id.checbox2:
//do logic etc...
break;
}
}
then use a switch case deal between different click event from user
hope it help
Of course there is a simpler way. Just make your Activity where you are doing this implement OnCheckedChangeListener
public Class MyActivity extends Activity implements OnCheckedChangeListener{
//your activity logic in here
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//your Activity will now receive onCheckedChangeEvents here from
//the checkboxes to which you assign it as a listener
}
}
, in onCreate, after you get references to all of your day checkboxes, set the Activity as the listener like this
#Override
public void onCreate(Bundle savedInstanceState){
//first get references to all your checkboxes and assign them to mon, tue, wed etc.
//then set the listeners
mon.setOnCheckedChangeListener(this);
tue.setOnCheckedChangeListener(this);
wed.setOnCheckedChangeListener(this);
thu.setOnCheckedChangeListener(this);
fri.setOnCheckedChangeListener(this);
sat.setOnCheckedChangeListener(this);
sun.setOnCheckedChangeListener(this);
}
, make sure all of your checkboxes have IDs assigned to them in the layout xml, like this
<CheckBox
android:id="#+id/checkMonday"
....
, then you will have one onCheckedChange method where you can handle the different days like this
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
counter++; //a day has been checked
}else{
counter--; //a day has been unchecked
}
switch(id){
case R.id.checkMonday:
//logic for Monday
break;
case R.id.checkTuesday:
//logic for Tuesday
break;
...
}
}
That should do the trick!

How to implement a CheckBox's onTouchEvent

How do you implement this onTouchEvent? It should fire when the user checks or unchecks the CheckBox widget.
CheckBox checkBox = new CheckBox(activity);
checkBox.setText("Don't present me information again.");
checkBox.onTouchEvent(.....);
The CheckBox widget (and any other widget that extends CompoundButton) has a method setOnCheckedChangeListener, which is the bit you're lacking (you probably don't want to use onTouchEvent in this case).
This example should replace the final line of code in your snippet:
checkBox.setOnCheckedChangeListener( new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if ( isChecked ) {
// do some stuff
}
}
});
what you need in your case is this
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
Toast.makeText(yourActivity.this,"CheckBox is Checked ..",3000).show();
}
else{
Toast.makeText(yourActivity.this,"CheckBox is Unchecked ..",3000).show();
}
}
});

Categories

Resources