How to validate which checkbox is checked? - android

I am new to android ,Here I have 7 check boxes in my application and I have customized it as in the image .Each checkbox is represents a day of the week .
What I want to do is ,If a user clicked on a checkbox the text of the clicked checkbox should appear on the above TextView (by default if there is any checkbox is not clicked Textview text should be as "Never").
Here the Textview text should be in a ordered way ,I mean If I select all the check boxes randomly but the TextView text should be in a ordered way like Sun,Mon,.....Sat.
I know how to validate a checkbox is checked or not ,but When it comes to the above situation I don't know how it should be .
Can anyone help me to get this .
private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
public void onClick(View v) {
switch (v.getId()) {
case R.id.mSun:
if (mRepeat.getText().toString().contains("Sun")) {
mAlarmDays.remove(0);
Toast.makeText(getApplicationContext(),"Sun is clicked",Toast.LENGTH_LONG).show();
} else
mAlarmDays.put(0, "Sun");
break;
case R.id.mMon:
if (mRepeat.getText().toString().contains("Mon")) {
mAlarmDays.remove(1);
} else
mAlarmDays.put(1, "Mon");
break;
case R.id.mTue:
if (mRepeat.getText().toString().contains("Tue")) {
mAlarmDays.remove(2);
} else
mAlarmDays.put(2, "Tue");
break;
case R.id.mWed:
if (mRepeat.getText().toString().contains("Wed")) {
mAlarmDays.remove(3);
} else
mAlarmDays.put(3, "Wed");
break;
case R.id.mThu:
if (mRepeat.getText().toString().contains("Thu")) {
mAlarmDays.remove(4);
} else
mAlarmDays.put(4, "Thu");
break;
case R.id.mFri:
if (mRepeat.getText().toString().contains("Fri")) {
mAlarmDays.remove(5);
} else
mAlarmDays.put(5, "Fri");
break;
case R.id.mSat:
if (mRepeat.getText().toString().contains("Sat")) {
mAlarmDays.remove(6);
} else
mAlarmDays.put(6, "Sat");
break;
}
StringBuilder repeatDays = new StringBuilder();
if (mAlarmDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (String day:mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
mRepeat.setText(repeatDays.toString());
}

I suggest to create one checkbox listener for all checkboxes which will iterate over them and build string for textview
Something like this:
CheckBox sunday = ...;
CheckBox monday = ...;
//......
CheckBox saturday = ...;
TextView label = ...;
CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
List<String> days = new ArrayList<>();
if (sunday.isChecked()) {
days.add("Sun");
}
if (monday.isChecked()) {
days.add("Mon");
}
//....
if (saturday.isChecked()) {
days.add("Sat");
}
if (days.size() != 0) {
label.setText(TextUtils.join(", ", days));
} else {
label.setText("Never");
}
}
};
sunday.setOnCheckedChangeListener(changeListener);
monday.setOnCheckedChangeListener(changeListener);
//......
saturday.setOnCheckedChangeListener(changeListener);

It really depends how you have the text set... but you can do something like.
if (checkBox.isChecked()) {
System.out.println("checkBox clicked");
}

try this for every checkbox.
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()){
Log.i("Checkbox", "Checkbox selected.");
}else{
Log.i("Checkbox", "Checkbox Not selected.");
}

Related

How to add text to TextView without replacing the old text?

As I mentioned in the title ,I want to add text to a Textview without replacing the previous text .
In my application I have a TextView and 7 buttons .On every button click I set the text of button to the TextView.
If the button is clicked on first time ,Setting the text to TextView ,and if the same button is clicked 2nd time I am removing that button's text from TextView.
Here What I want to do is for 7 buttons I want to set positions(uniqueness for sun to sat) in TextView and when the respective button is clicked that text is set to the TextView and if the button is clicked 2nd time that specific position of the text should remove .
Here text shouldn't replace the previous text that is important to have and if some button's are selected and again that are deselected means TextView should show the default text as "Never"
I tried to get source from SO but I can't find a clear solution for this .
If anyone helps me to come out from this ,that's much helpful for me .
coding
public class CreateAlarm extends Activity implements View.OnClickListener {
private Button mbtn_Sun, mbtn_Mon, mbtn_Tue, mbtn_Wed, mbtn_Thu, mbtn_Fri, mbtn_Sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_alarm);
mRepeat = (TextView) findViewById(R.id.mRepeat);
mbtn_Sun = (Button) findViewById(R.id.mbtn_Sun);
mbtn_Mon = (Button) findViewById(R.id.mbtn_Mon);
mbtn_Tue = (Button) findViewById(R.id.mbtn_Tue);
mbtn_Wed = (Button) findViewById(R.id.mbtn_Wed);
mbtn_Thu = (Button) findViewById(R.id.mbtn_Thu);
mbtn_Fri = (Button) findViewById(R.id.mbtn_Fri);
mbtn_Sat = (Button) findViewById(R.id.mbtn_Sat);
mbtn_Sun.setOnClickListener((View.OnClickListener) this);
mbtn_Mon.setOnClickListener((View.OnClickListener) this);
mbtn_Tue.setOnClickListener((View.OnClickListener) this);
mbtn_Wed.setOnClickListener((View.OnClickListener) this);
mbtn_Thu.setOnClickListener((View.OnClickListener) this);
mbtn_Fri.setOnClickListener((View.OnClickListener) this);
mbtn_Sat.setOnClickListener((View.OnClickListener) this);
int hours = mTimePicker.getCurrentHour();
mCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mRepeat.setText("");
} else
mRepeat.setText("Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mRepeat.setText("");
} else
mRepeat.setText("Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mRepeat.setText("");
} else
mRepeat.setText("Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mRepeat.setText("");
} else
mRepeat.setText("Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mRepeat.setText("");
} else
mRepeat.setText("Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mRepeat.setText("");
} else
mRepeat.setText("Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mRepeat.setText("");
} else
mRepeat.setText("Sat");
break;
default:
mRepeat.setText("Never");
}
}
}
Image :
By default the TextView text is "Never".
You can define a TreeMap as:
private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
as a field of your class and add/remove the days to/from the TreeMap when the corresponding button is clicked. So the implementation of onClick method will be:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mAlarmDays.remove(0);
} else
mAlarmDays.put(0, "Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mAlarmDays.remove(1);
} else
mAlarmDays.put(1, "Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mAlarmDays.remove(2);
} else
mAlarmDays.put(2, "Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mAlarmDays.remove(3);
} else
mAlarmDays.put(3, "Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mAlarmDays.remove(4);
} else
mAlarmDays.put(4, "Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mAlarmDays.remove(5);
} else
mAlarmDays.put(5, "Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mAlarmDays.remove(6);
} else
mAlarmDays.put(6, "Sat");
break;
}
StringBuilder repeatDays = new StringBuilder();
if (mAlarmDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (String day:mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
mRepeat.setText(repeatDays.toString());
}
You should set each button id first,add this to your xml for each specific button : android:id="sun" and ...
My suggestion is: use a single TextView can make your logic quite complex
Use a horizontal LinearLayout instead, you will have 7 TextView inside with predefine text and position. Just simply show/hide them according to which button is clicked and you don't have to deal with any complex string analize.

Issues passing Edit text into an If statement

I have a quiz app that should contains the following:
1- radio groups/buttons
2- checkbox
3- editText
all should calculate a score. I manage to get the radio groups, checkbox to work and calculate the score. however having problems with the editText
I am trying to compare the value of what's in name2 variable but I am unable to do so. I've implemented the .equals syntax but it's not working accordingly.
Any suggestions/solutions are grateful.
public class Quiz extends AppCompatActivity {
int score = 0;
String name;
String name2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
}
public void onRadioButtonCliked(View view) {
Boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.yes_radio_buttonG1B3:
if (checked)
score++;
break;
case R.id.yes_radio_buttonG2B2:
if (checked)
score++;
break;
case R.id.yes_radio_buttonG3B2:
score++;
if (checked)
break;
case R.id.yes_radio_buttonG4B1:
if (checked)
score++;
break;
case R.id.yes_radio_buttonG5B2:
if (checked)
score++;
break;
case R.id.yes_radio_buttonG6B3:
if (checked)
score++;
break;
}
}
public void onCheckBoxCliked(View view) {
Boolean checked = ((CheckBox) view).isChecked();
CheckBox cB1 = (CheckBox) findViewById(R.id.checkBox1);
CheckBox cB2 = (CheckBox) findViewById((R.id.checkBox2));
CheckBox cB3 = (CheckBox) findViewById(R.id.checkBox3);
if (cB1.isChecked() && cB2.isChecked() && !cB3.isChecked()) {
score++;
}
}
public void EditText(View view) {
EditText text = (EditText) findViewById(R.id.editText1);
name2 = text.getText().toString();
if (name2.equals("8")) {
score++;
}
}
public void submitOrder(View view) {
EditText username = (EditText) findViewById(R.id.editText);
name = username.getText().toString();
Toast.makeText(this, "Thank you " + name, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "You have got 8/" + score, Toast.LENGTH_SHORT).show();
}
}
Since it's a string variable, you must use "" to indicate it's a string.
Whole number are only applicable for int variable type.
if(name2.equals("8")){
score++;
}
Try replacing the 8 with "8" as you are supposed to be comparing a String, not an integer.
As already mentioned in a comment and in the two other answers, you can't compare a String to a number.
Instead of checking a String, convert that string to an integer(assuming it is a valid int) and compare the ints instead.
First, convert:
int value;
try{
value = Integer.parseInt(name2);//throws an exception if it isn't a valid integer or if name == null
}catch(Exception e){
return;//not valid, don't continue
}
Then compare:
if(value == 8){
score++;
}

checkbox text not getting displayed on textview

I am new to android technology, i wanted the checkbox's text to be displayed using textview.
I think the problem is in TextUtils.join function
For e.g
If my checklist is a, b, c, d.
I want to display the textview like
//Text view string
you have selected a, b, c, d.
Code Snippet :
public class Menu3 extends AppCompatActivity {
CheckBox checkBox;
CheckBox checkBox2;
CheckBox checkBox3;
CheckBox checkBox4;
TextView tView;
ArrayList<String> arrayList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu3);
checkBox = (CheckBox)findViewById(R.id.checkBox);
checkBox2 = (CheckBox)findViewById(R.id.checkBox2);
checkBox3 = (CheckBox)findViewById(R.id.checkBox3);
checkBox4 = (CheckBox)findViewById(R.id.checkBox4);
tView = (TextView)findViewById(R.id.textView3);
checkBox.setOnClickListener(ocl);
checkBox2.setOnClickListener(ocl);
checkBox3.setOnClickListener(ocl);
checkBox4.setOnClickListener(ocl);
String all = TextUtils.join(",", arrayList);
tView.setText(all);
}
View.OnClickListener ocl = new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean checked = ((CheckBox) v).isChecked();
if(checked) {
String text = null;
switch (v.getId()) {
case R.id.checkBox:
text = checkBox.getText().toString();
Toast.makeText(Menu3.this, text + " checked.", Toast.LENGTH_SHORT).show();
arrayList.add(text);
break;
case R.id.checkBox2:
text = checkBox2.getText().toString();
Toast.makeText(Menu3.this, text + " checked.", Toast.LENGTH_SHORT).show();
arrayList.add(text);
break;
case R.id.checkBox3:
text = checkBox3.getText().toString();
Toast.makeText(Menu3.this, text + " checked.", Toast.LENGTH_SHORT).show();
arrayList.add(text);
break;
case R.id.checkBox4:
text = checkBox4.getText().toString();
Toast.makeText(Menu3.this, text + " checked.", Toast.LENGTH_SHORT).show();
arrayList.add(text);
break;
}
}
}
};
}
The problem is that you are calling setText right away in onCreate, and it isn't called again when the checkboxes are changed. Personally, I'd set up a method that gets called when a checkbox is changed to handle all of this
Something like this
private void updateTextView(String text) {
Toast.makeText(Menu3.this, text + " checked.", Toast.LENGTH_SHORT).show();
arrayList.add(text);
String all = TextUtils.join(",", arrayList);
tView.setText(all);
}
Then call it from all of your OnClickListener for the checkboxes
case R.id.checkBox:
updateTextView(checkBox.getText().toString());
...
You are setting the text first, after then, you are not setting anything. You must set text every time you add/remove string to your arraylist.

How to do addition or substraction of two dynamic check boxes values

I have dynamic check boxes which are having values like
check box 1 = $ 20
check box 2 = $ 10
if i click one check box then the value should reflect in a text view and if i select two check boxes then the value should be added to that text view or deselect the check box the value should be subtracted from that text view.
This check box values are coming dynamically so i need to add/subtract those two values and show the total value in the text view.
How can i do that please help me.
Thanks in advance
Hear is Tested code for this and working fine..
try this
public class MainActivity extends Activity implements OnCheckedChangeListener {
CheckBox cb1, cb2;
TextView tv1;
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = (CheckBox) findViewById(R.id.checkBox1);
cb2 = (CheckBox) findViewById(R.id.checkBox2);
tv1 = (TextView) findViewById(R.id.textView1);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == R.id.checkBox1) {
if (isChecked) {
count = count + Integer.parseInt(cb1.getText().toString());
} else {
if (count != 0) {
count = count - Integer.parseInt(cb1.getText().toString());
}
}
tv1.setText("" + count);
}
if (buttonView.getId() == R.id.checkBox2) {
if (isChecked) {
count = count + Integer.parseInt(cb2.getText().toString());
} else {
if (count != 0) {
count = count - Integer.parseInt(cb2.getText().toString());
}
}
tv1.setText("" + count);
}
}
}
Good Luck..
for check box we have one event listener as OnCheckedChangeListener
override this listener for both of your Checkboxes(e.g chkBox1, chkBox2), and
check
int value1,value2;
if(chkBox1.isChecked){
//store value of this checkbox into one variable i.e.
value1 = value of chkBox1;
}else{
// reset value for this chkBox1 i.e
value1 = 0;
}
if(chkBox2.isChecked){
//store value of this checkbox into one variable i.e.
value2 = value of chkBox2;
}else{
// reset value for this chkBox2 i.e
value2 = 0;
}
int sum = value1 + value2;
textView.setText(sum + "");
Use OnCheckedChangeListeners to do your task
chkBox1.setOnCheckedChangeListener(eventListener);
chkBox2.setOnCheckedChangeListener(eventListener);
CompoundButton.OnCheckedChangeListener eventListener=new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId())
{
case R.id.chk1;
if(isChecked)
{
//do your first task
//set your textbox values
}
break;
case R.id.chk2:
if(isChecked)
{
//do your second task
//set your second text values
}
break;
}
}
};
if you dont have t0o many check box and iteration is fine than you can try following code.
public void addUserType(ProObj obj)
{
final CheckBox cb = new CheckBox(getActivity());
cb.setText(obj.getamount());
cb.setTag(obj);
layUsertype.post(new Runnable() {
public void run() {
layUsertype.addView(cb);
}
});
//any listener method
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
double sum = 0.0;
int childcount = layUsertype.getChildCount();
for (int i=0; i < childcount; i++){
View v = layUsertype.getChildAt(i);
if(v instanceof CheckBox)
{
CheckBox cb = (CheckBox)v;
if(cb.isChecked())
{
if(cb.getTag() instanceof ProObj )
{
sum+=((UserTypesObj) cb.getTag()).getamount();
}
}
}
}
}
//update logic
});
}

click a button for diffrent time

I've a Button for 8 CheckBoxs and i want when i click on button a piece of my code running for example case 1 in this code for showing tick on chk1 (note.done1) .and for second time when i clicked on button,my app recognize chk1 is checked and now checking chk2(note.done2) and too for 6 another CheckBoxs.
but this code is wrong because when i click on button, chk1 and chk3 and chk5 are true.also I'm trying else if and Sharedpreferences and array .
i can't use ischecked() method because i use listview with 1000(in less) item.just i can use note.done where link to checkboxs in each item .i can use array where link to each checkboxs and sqlite too!
so can anyone help me?
btnOk.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
switch (G.result_s1[position]) {
case 0: {
note.done1 = true;
note.c = Color.RED;
G.database_s.execSQL("UPDATE mystate SET s1='1' WHERE s_id=" + (position + 1));
G.result_s1[position] = 1;
//AdapterNote.img.setBackgroundColor(Color.CYAN);
}
break;
case 1:
{
note.done2 = true;
note.c = Color.GREEN;
G.database_s.execSQL("UPDATE mystate SET s2='1' WHERE s_id=" + (position + 1));
G.result_s2[position] = 1;
}
break;
}
switch (G.result_s3[position]) {
case 0: {
note.done3 = true;
note.c = Color.MAGENTA;
G.database_s.execSQL("UPDATE mystate SET s3='1' WHERE s_id=" + (position + 1));
G.result_s3[position] = 1;
}
break;
case 1: {
note.done4 = true;
note.c = Color.YELLOW;
G.database_s.execSQL("UPDATE mystate SET s4='1' WHERE s_id=" + (position + 1));
G.result_s4[position] = 1;
}
break;
}
dialog.dismiss();
finish();
}
});
Why don't you use ischecked()?
Whenever you click the button you check another checkbox
btnOk.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(!checkbox1.ischecked()){
// check checkbox1
}else if(checkbox1.ischecked()){
// check checkbox2
}else if(checkbox1.ischecked() && checkbox2.ischecked()){
// check checkbox3
}....
}
}

Categories

Resources