Android "Toggle off all buttons" - android

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);
....
}

Related

How to change switch conditions using Toggle button?

I have a program which creates a list of to-dos that allows the user to set a date/time for the app to notify them about it. There's a checkbox that says 'Notify me' which is supposed to schedule the notification. But, in the main RecyclerView of the list, there is also a toggle switch that allows the user to turn off/on the notification after they've saved it. Problem is, the toggle switch doesn't seem to be changing the notification state.
holder.notifSwitch.setChecked(journalModel.isNotify());
if(journalModel.getJournalDateNotify().getTime() > System.currentTimeMillis())
{
holder.notifSwitch.setVisibility(View.VISIBLE);
} else {
holder.notifSwitch.setVisibility(View.INVISIBLE);
}
holder.notifSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!journalModel.isNotify())
{
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(),true);
} else
{
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(), false);
}
}
});
And here is the code for updating the Realm object:
public boolean updateJournal (Realm realm, final int realmJournalNo, final boolean isNotify){
success = false;
try{
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
final TblJournal tblJournal = realm.where(TblJournal.class).equalTo("realmJournalNo", realmJournalNo).findFirst();
tblJournal.setNotify(isNotify);
success = true;
}
});
}catch (RealmException e){
Log.i("INFO","update Retail Exception: "+e.toString());
success = false;
}finally {
return success;
}
}
As per your code your are not updating journalModel.isNotify= true or false in on click. journalModel.setIsNotify(true):/journalModel.setIsNotify(false):
holder.notifSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// status="true"; //edit here
switch_btn.setChecked(true);
} else {
// status="false"; //edit here
switch_btn.setChecked(false);
}
}
});
Everything looks fine, you just need to modify your onClick method.
holder.notifSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v;
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(),checkbox.isChecked());
}
});

How I can change state of widget switch from check to uncheck?

I want to recieve data from thingspeak and change automatically state of switch from checked to unchecked or vice versa.
This is my code, but it doesn't work, just can set text but can't change state.
Please help me!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
myswitch1 = (Switch) findViewById(R.id.switch1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
postdata2();
}
});
t.start();
now_da1.setText(get_da1);
if(get_dk1 == "1") {
myswitch1.setChecked(False);
}
else if (get_dk1 =="0") {
myswitch1.setChecked(True);
}
}
});
myswitch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
t1 = "1";
} else {
t1 = "0";
}
}
});
public void postdata2() {
HttpRequest mReq = new HttpRequest();
get_t1 = mReq.sendGet("https://thingspeak.com/channels/106453/field/1/last");
get_da1 = get_t1.substring(0, 2);
get_dk1 = get_t1.substring(3, 3);
Log.d(myGet, get_dk1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
use this myswitch1.setChecked(false); to uncheck and myswitch1.setChecked(true); to make it checked.
Happy Coding :)
When comparing the value of two Strings always use the equals() method which compares the values instead of using == which compares the object references.
For example:
String first = "1";
String second = "1";
first == second will return false because they are not referencing to the same object. However, first.equals(second) will return true because the value of the two Strings are identical.
You can try replacing the conditions with this:
if (get_dk1.equals("1")) {
myswitch1.setChecked(false);
} else if (get_dk1.equals("0")) {
myswitch1.setChecked(true);
}

Get buttons to do something when they are checked

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.

How to create single check box event for all check boxes in android?

I'm new to android it is possible in .net, But i want to build this logic in android is it possible to do that please help me, i know only creating check box event for each check box instead of that i want to create single event for all Check boxes please help me.
Vb.net
private sub Chkbox1_Checkchanged(byval sen as object,byval e as system.eventargs) handles Chkbox1.checked,chkbox2.checked,chkbox3.checked
blnvalue=true
end sub
android
chkbox1= (CheckBo1) findViewById(R.id.chkbox1);
rbtn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
blnvalue=true;
}
});
implements onCheckChangedEvent for for all checkBox.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == activity.getCheckRefresh()) {
activity.setRefreshRate("1");
activity.setEnable(isChecked);
return;
}
}
Assuming you are in an Activity, you can make your Activity implement View.OnClickListener, and then use this for each View within your Activity, even ones that aren't CheckBox.
public class MyActivity extends Activity implements View.OnClickListener {
//declare checkboxes
public void onCreate(Bundle something) {
//setup stuff and get views
rbtn1.setOnClickListner(this);
rbtn2.setOnClickListner(this);
rbtn3.setOnClickListner(this);
rbtn4.setOnClickListner(this);
}
public void onClick(View v) {
if(v == rbtn1) {
//do stuff for checkbox1
} else if(v == rbtn2) {
//do stuff for checkbox2
} else if(v == rbtn3) {
//do stuff for checkbox3
} else if(v == rbtn4) {
//do stuff for checkbox4
}
}
}
You can also use onChackChanged Event & OnClickEvent for checkbox.
chkbox1= (CheckBo1) findViewById(R.id.chkbox1);
chkbox2= (CheckBo1) findViewById(R.id.chkbox2);
chkbox1.setOnClickListener(this);
chkbox1.setOnCheckedChangeListener(this);
chkbox2.setOnClickListener(this);
chkbox2.setOnCheckedChangeListener(this);
public void onClick(View v)
{
if(v.getId() == R.id.chkbox2)
// your code
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == activity.getCheckRefresh()) {
activity.setRefreshRate("1");
activity.setEnable(isChecked);
return;
}
}
u can do this.. here click is an inner class.. and create an instance of it n pass it to every setOnClickListener(click)
class click implements OnClickListener
{
public void onClick(View v)
{
// your code
}
}

Radio button cannot be unchecked

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;
}
}
});

Categories

Resources