I got multiple buttons which are managed by this code:
ToggleButton.OnCheckedChangeListener listener = new ToggleButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
int index;
for(index = 0; index < viewIds.length; index++) {
if (v.getId() == viewIds[index]) {
getSharedPreferences("PREFS_NAME", MODE_PRIVATE)
.edit()
.putBoolean(stringIds[index], isChecked)
.apply();
break;
}
}
}
};
i've created method
scanButton();
like this:
private void scanButton() {
Button scan = (Button)findViewById(R.id.button_scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//here what will be done when button is clicked
Log.d(TAG, "Button \"SCAN\" is pressed");
Toast.makeText(RandDActivity.this, "Scan pressed", Toast.LENGTH_LONG)
.show();
//WHAT HERE
}
});
}
now Question: what to put here to get only those buttons which are isChecked with "true" state to do something?
Firstly you need to see whether the user has checked the button or not for that you can Load your SharedPreferences that you already saved in your onCheckedChanged() method, save it to a Boolean variable and then put a condition Like this:
if(isChecked){
//do your stuff with the button
}
I am assuming isChecked is your boolean variable that you just set with the value that you got from SharedPreferences.
Related
I added a switch button to my app and created a setOnCheckChangeListener the will pop up toast message..
But it works only after the second click.
Any ideas?
MainActivity:
public void checkSwitch (View view)
{
addBeeps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
beeps = true;
Toast.makeText(MainActivity.this,"beeps is True",Toast.LENGTH_LONG).show();
}else
{
beeps = false;
Toast.makeText(MainActivity.this,"beeps is False",Toast.LENGTH_LONG).show();
}
}
});
}
I think in xml file which contains addBeeps switch you are using this code.
<Switch
android:id="#+id/addBeeps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="checkSwitch"/>
Root cause: For the first time when you check the switch, a listener will be set on it and do nothing, the second time when you check the switch again the listener is triggered and the Toast showed.
Solution: Here is a solution for you.
Remove onClick from the switch in xml file
<Switch
android:id="#+id/addBeeps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Delete or comment-out checkSwitch method from MainActivity and move set listener block code to onCreate method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addBeeps = findViewById(R.id.addBeeps);
// TODO: Put set listener block code here
addBeeps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
beeps = true;
Toast.makeText(MainActivity.this, "beeps is True", Toast.LENGTH_LONG).show();
} else {
beeps = false;
Toast.makeText(MainActivity.this, "beeps is False", Toast.LENGTH_LONG).show();
}
}
});
}
// TODO: Delete or comment-out this method
// public void checkSwitch(View view) {
// addBeeps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// #Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// if (isChecked) {
// beeps = true;
// Toast.makeText(MainActivity.this, "beeps is True", Toast.LENGTH_LONG).show();
// } else {
// beeps = false;
// Toast.makeText(MainActivity.this, "beeps is False", Toast.LENGTH_LONG).show();
// }
// }
// });
// }
I am working on android. I have a checkbox in my app and I want to save a custom text against my checkbox when it is checked or not.
CheckBox atbSealedCheckBox;
String selectedAtbSealedValue ="";
atbSealedCheckBox = (CheckBox) view.findViewById(R.id.atbCheckBox);
Now, If I checked the checkbox then I want to store Yes otherwise No. Also, I want to store it in my DB. For this I have already created get and set methods in my Model
private String atbSealed;
public String getAtbSealed(){return atbSealed;}
public void setAtbSealed(String atbSealed){this.atbSealed=atbSealed;}
Update 1
As per suggestion, I have tried below
atbSealedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(isatbSealedChecked)
{
selectedAtbSealedValue = "Yes";
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = true;
}
else
{
selectedAtbSealedValue = "No";
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = false;
}
}
});
But It doesn't help me out. Also, the ctQuantitySpinner is not disabled on atbSealedCheckbox checked. Also if checked the boolean value of the checkbox is not changed to true. Although in log I do see the selectedAtbSealedValue but it's not set in the app. In my SaveDataLocal() function I have set the value like below
survey.setAtbSealed(this.selectedAtbSealedValue);
SurveyManager dbHelper = new SurveyManager(getActivity());
dbHelper.addSurvey(survey);
I have also tried the below code
atbSealedCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox)v).isChecked())
{
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
selectedAtbSealedValue = "Yes";
isatbSealedChecked = true;
//atbCheckBoxEdittext.setText(selectedAtbCheckBox);
}
else {
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
selectedAtbSealedValue = "No";
isatbSealedChecked = false;
//atbCheckBoxEdittext.setText(selectedAtbCheckBox);
}
}
});
This code shows me the selected checkbox value, disabled the spinner and set the spinner value to null. but again the checkbox value is not saved.
How can I save custom text in a string?
Try this, it work for me:
atbSealedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
if (isChecked) {
selectedAtbSealedValue = "yes";
} else {
selectedAtbSealedValue = "no";
}
}
} );
After that, you can store selectedAtbSealedValue into DB;
Update: Your fixed code:
atbSealedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b)
{
selectedAtbSealedValue = "Yes";
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = true;
}
else
{
selectedAtbSealedValue = "No";
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = false;
}
}
});
As long as I understand your question, you want to change this variable String selectedAtbSealedValue =""; when user checked your checkbox.
To achieve this statement, you can use onCheckedListener that is triggered as soon as users checked or removed the check from your checkbox.
atbSealedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
// Todo something.
}
} );
This will help you.
At submit button get the current value of checkbox.
submit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
control_of_vectors_value = control_of_vectors_checkbox.isChecked() == true ? "yes" : "no"; }}
I am making an app that toggles off and on a switch. I have 4 switches in total and at the bottom I would like to have a button that will toggle them all off at the same time - like an override switch. I'm trying to follow the same format as when creating the 4 switches, but I can't wrap my head around how it would be. I already tried looking through stackOverFlow and I can't find anything on it, maybe I just don't know the key words.
Switch toggleapp1 = (Switch) findViewById(R.id.app1);
toggleapp1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggleapp1(true);
Toast.makeText(getApplicationContext(), "[Application1] Enabled!", Toast.LENGTH_LONG).show();
} else {
toggleapp1(false);
Toast.makeText(getApplicationContext(), "[Application1] Disabled!", Toast.LENGTH_LONG).show();
}
}
});
Is how one of the switches look. toggleapp1 is switched with 2,3,4.
public boolean toggleapp1(boolean status) {
if (status == true) {
return true;
}
else if (status == false) {
return false;
}
return status;
}
I have 4 switches in total and at the bottom I would like to have a button that will toggle them all off at the same time - like an override switch.
If i understood the problem you have something like:
toggleapp1 = (Switch) findViewById(R.id.app1);
toggleapp2 = (Switch) findViewById(R.id.app2);
toggleapp3 = (Switch) findViewById(R.id.app3);
toggleapp4 = (Switch) findViewById(R.id.app4);
And you want to disable all of them togheter.
You can create a method that do this:
private toggleOffSwitches(boolean state) {
toggleapp1.setChecked(state);
toggleapp2.setChecked(state);
toggleapp3.setChecked(state);
toggleapp4.setChecked(state);
}
And call it in the OnClickListener of the button:
Button yourButton = (Button) findViewById(R.id.yourButton);
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
toggleOffSwitches(false);
}
});
Remember to declare your Switches as field class variables in order to use them in the toggleOffSwitches method!
UPDATE
For example:
public class MainActivity extends Activity {
private Switch toggleapp1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
toggleapp1 = (Switch) findViewById(R.id.app1);
....
}
I want to inspect something before CheckBox's state changed. If user has logged in, change the state and post data to server. Otherwise, keep the state and show a log-in activity.
OnCheckedChangeListener does not fit. Is there anything like PreferenceChangeListener (return false to keep stateļ¼ in CheckBox.
Or other methods.
Thank you!
Update:
OnClickListener work for me.
#Override
public void onClick(View view) {
boolean isChecked = checkBox.isChecked();
checkBox.setChecked(!isChecked);
if (loggedIn) {
checkBox.setChecked(isChecked);
// push data to server
} else {
// show log-in activity
}
}
If I'm not misunderstood your question, you can still achieve it by using OnCheckedChangeListener:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isLogin) {
buttonView.setChecked(true);
Toast.makeText(this, "Sending data", Toast.LENGTH_SHORT).show();
} else {
buttonView.setChecked(false);
Toast.makeText(this, "Show login activity", Toast.LENGTH_SHORT)
.show();
}
}
You can do something like this:
CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
or if you just want to see if the checkbox has been checked or not, then you can use this
CheckBox checkBox = (CheckBox)v.findViewById(R.id.Checkbox);
checkBox.setChecked(!checkBox.isChecked());
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// awesome code you may use
// if (isChecked)
// (buttonView.getId() == R.id.check1 ? check2 : check1)
// .setChecked(false);
//Or use the code below
if (isChecked) {
switch (buttonView.getId()) {
case R.id.check1:
check2.setChecked(false);
Toast.makeText(getApplicationContext(), "Check1 is selected!!! =)",
Toast.LENGTH_LONG).show();
break;
case R.id.check2:
check1.setChecked(false);
Toast.makeText(getApplicationContext(), "Check2 is selected!!! =)",
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
I have created a radio button in my app. Now when radio button isChecked I call a function, and it's working fine. But my problem is that once I checked the radio button then and again click of that radio button doesn't make it unchecked.
So, how that can be done?
My code:
super.onCreate(savedInstanceState);
setContentView(R.layout.setting_frm);
rb1=(RadioButton)findViewById(R.id.option1);
rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton v, boolean arg1) {
// TODO Auto-generated method stub
if(rb1.isChecked() == true)
t1.setText("Selected is : "+rb1.getText());
if(rb1.isChecked() == false)
t1.setText("Selected is : ");
}
});
t1=(TextView)findViewById(R.id.TextView01);
}
You can't do that with a radio button. Go for CheckBox.
Try this
you can use boolean Flag option ..here is the code
boolean flag = false;
rb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(flag){
rb.setChecked(false);
flag = false;
}
else{
rb.setChecked(true);
flag = true;
}
}
});