Whackyy Whackyy - 4 months ago 19
Android Question

Add all checked checkboxes android

my xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Select Products"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/buy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="92dp"
android:text="Pay" />

<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/cb2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:text="Casual Shirt - 400 Rs" />

<CheckBox
android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/buy"
android:layout_alignLeft="@+id/cb3"
android:layout_marginBottom="34dp"
android:text="t-Shirt - 200 Rs" />

<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/cb2"
android:layout_centerVertical="true"
android:text="Shorts - 300 Rs" />

<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/cb3"
android:layout_alignLeft="@+id/cb1"
android:layout_marginBottom="17dp"
android:text="Jeans - 500 Rs" />

</RelativeLayout>


java file

package com.example.multiactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MaleActivity extends Activity {

Button b1;
CheckBox cb1, cb2, cb3, cb4;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.male);

b1 = (Button) findViewById(R.id.buy);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);

b1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String added = "";

if (cb1.isChecked()) {

int a = 400;

}
if (cb2.isChecked()) {

int b = 500;
}
if (cb3.isChecked()) {

int c = 300;
}
if (cb4.isChecked()) {

int d = 200;
} else {

return;

}

}

});

}

}


I am trying to add all selected checkboxes.

I am trying to get the total of all added products.

I am unable to select all checkedboxes for the total figure.

I want to add the selected int and get the total

ex. int a + b + c;

Help

Thanks.

Answer

I guess your onClick() method could be this instead

    String added;
    @Override
    public void onClick(View v) {

        int a = 0;
        if (cb1.isChecked()) a+=400;
        if (cb2.isChecked()) a+=500;
        if (cb3.isChecked()) a+=300;
        if (cb4.isChecked()) a+=200;
        added = Integer.toString(a);
    }

I don't really know why the checkboxes wouldn't work though.

Comments