Reputation: 9
I want to know how many Switch is enable at same time and print Their Values in Toast, How can i do it
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class ListItem extends AppCompatActivity {
Switch s1,s2,s3,s4;
int Switch;
Switch[] switches = new Switch[]{s1,s2,s3,s4};
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
s1 = findViewById(R.id.switch1);
s2 = findViewById(R.id.switch2);
s3 = findViewById(R.id.switch3);
s4 = findViewById(R.id.switch4);
submit = findViewById(R.id.clickbtn);
s1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
String result1 = "Table";
}
}
});
s2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
String result1 = "Chair";
}
}
});
s3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
String result1 = "Laptop";
}
}
});
s4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
String result1 = "Desktop";
}
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 1; i <= switches.length; i++) {
s
// Toast.makeText(getApplicationContext(), "You picked Table and chair", Toast.LENGTH_LONG).show();
}
}
});
}
}
I have a list of some item if user checked three switches Then user Got A Toast message with value like you checked Three item, please find the code i am trying but i have only static method to check these possibility.
Upvotes: 0
Views: 34
Reputation: 396
No need for setOnCheckedChange listener. Try this. Or customize it however you want
s1 = findViewById(R.id.switch1);
s2 = findViewById(R.id.switch2);
s3 = findViewById(R.id.switch3);
s4 = findViewById(R.id.switch4);
s1.setTag("Table1");
s2.setTag("Table2");
s3.setTag("Table3");
s4.setTag("Table4");
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ss = "You picked ";
for (Switch s: switches) {
if(s.isChecked())
ss+=s.getTag() + ", " ;
}
Toast.makeText(getApplicationContext(), ss, Toast.LENGTH_LONG).show();
}
};
Upvotes: 1
Reputation: 342
What you need to do is check the 3 switches status on the onClickListener
of the submit Button
. If the switch is already isChecked
, then include it's text on a string for the Toast. You're already doing it. Just continue your code.
Upvotes: 0