So the thing I am trying to accomplish should be exactly like this : http://www.javascriptbank.com/simple-javascript-auto-sum-with-checkboxes.html/en/
Unfortunately im not shure if thats even possible so I would appreciate any kind of help or idea . Thanks in advance.Some thing that i have so far:
CheckBox chkone;
CheckBox chktwo;
CheckBox chkthree;
TextView tv;
OnClickListener checkBoxListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chkone = (CheckBox)findViewById(R.id.chk1);
chktwo = (CheckBox)findViewById(R.id.chk2);
thkthree = (CheckBox)findViewById(R.id.chk3);
checkBoxListener = new OnClickListener() {
//#Override
public void onClick(View v) {
tv = (TextView)findViewById(R.id.tv1);
if (chkone.isChecked())
{
// something
}
}
};
chkone.setOnClickListener(checkBoxListener);
chktwo.setOnClickListener(checkBoxListener);
chkthree.setOnClickListener(checkBoxListener);
You should implement the CompoundButton's OnCheckedChangeListener interface instead, to get notifications about the checkbox's state changes.
Assuming that you didn't extend the CheckBox class to ease your work by storing a numeric value somewhere, you need to parse the label (text) of the checkbox in order to access it's value, and based on the checked state increase / decrease the sum:
private TextView sumText;
private float currentSum = 0;
private final OnCheckedChangeListener checkBoxListener =
new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
currentSum += getNumber(buttonView);
else
currentSum -= getNumber(buttonView);
// TODO: you need to initialize this TextView in your
// onCreate method!
sumText.setText(Float.toString(currentSum));
}
};
private float getNumber(final CompoundButton chk)
{
try
{
return Float.parseFloat(chk.getText().toString());
}
catch (NumberFormatException e)
{
return 0;
}
}
Note, that for this to work as you expect it, you should modify the getNumber method to retrieve the values that is actually under that CheckBox instance!
Related
I have an issue like selecting and unselecting a single radio button on click, the setOnCheckChangeListner() works only for the first time below is the code which I have tried.
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) radioButton.setChecked(false);
else radioButton.setChecked(true);
}
});
I have made a solution for it using set selected attribute of the radio button.
final RadioButton radioButton = findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!radioButton.isSelected()) {
radioButton.setChecked(true);
radioButton.setSelected(true);
} else {
radioButton.setChecked(false);
radioButton.setSelected(false);
}
}
});
try to use preference xml this one is essay to save the click events.because its like small db.
How to create RadioButton group in preference.xml window?
You are generating a endless loop. That will cause stackoverflow error.
Because you are changing radio button checked status inside onCheckedChanged.
If you need changing check status then do it in click of a button.
Don't change check status inside onCheckedChanged
Perhaps you need to change status of RadioButton for some condition. For that you will do following.
public class MainActivity extends AppCompatActivity {
RadioButton radioButton = null;
CompoundButton.OnCheckedChangeListener listener;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO: 8/6/2018 your logics
}
};
radioButton.setOnCheckedChangeListener(listener);
changeStatus(radioButton, true);
}
private void changeStatus(RadioButton radioButton, boolean status){
radioButton.setOnCheckedChangeListener(null);
radioButton.setChecked(status);
radioButton.setOnCheckedChangeListener(listener);
}
}
When you need to change status then call
changeStatus(radioButton, true);
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
});
}
I have Event Message object(EventMO)..In this i have event id,name,place,date,time.....I have another one called Event Reminder days message object...In this i have type id,is selected....I used Event Reminder Days mo as list inside the EventMO....
This is my EventMO
private long eventId;
private long ringeeUserId;
private String text;
private String place;
private String eventDate;
private String startTime;
private String endTime;
private int isDelete;
private List<EventReminderDaysMO> eventReminderDaysMOs;
public List<EventReminderDaysMO> getEventReminderDaysMOs() {
return eventReminderDaysMOs;
}
public void setEventReminderDaysMOs(List<EventReminderDaysMO> eventReminderDaysMOs) {
this.eventReminderDaysMOs = eventReminderDaysMOs;
}
}
This is my event Reminder days MO
private long eventReminderDaysId;
private String eventReminderDate;
private int isSelected;
private int isMessageSent;
private int typeId;
This is my Check box code...Here i need to set type id as one for this check box....i cant directly use event reminder days MO..Because i need to pass inside EventMO as list to backend.....So how to set the value to type id,which is inside the Event MO
checkBox_aWeekBefore.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
final int response = 2;
//Here only i have a problem
//eventMO.setTypeId(response); ---i cant use this...it wont be allowed
new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... arg0) {
return eventDelegates.addEvent(eventMO,context);
}
}.execute(null, null, null);
Toast.makeText(OccasionActivity.this,
"Checked", Toast.LENGTH_LONG).show();
}}
});
Using OnCheckedChangeListener(), might solve your query. For further breakdown, follow the code snippet:
Implement the OnCheckedChangeListener, as follows:
class YourClass implements OnCheckedChangeListener{/.../}
Enable the listeners to each Checkbox, as follows:
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
cb4.setOnCheckedChangeListener(this);
Override the onCheckedChanged method, to listen to the changes in checkbox's state:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.getId(); //To get id of the checkbox
}
Hope that helps.
It is not a correct way to get checkBox result, you need to add OnCheckedChangeLisenter in the place of OnItemClick.. I am giving you sample code for it....
CheckBox chk1;
CheckBox chk2;
CheckBox chk3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
chk1 = (CheckBox) findViewById(R.id.chk1);
chk2 = (CheckBox) findViewById(R.id.chk2);
chk3 = (CheckBox) findViewById(R.id.chk3);
chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b == true) {
Toast.makeText(getApplicationContext(), "On Event Day Selected", Toast.LENGTH_LONG).show();
}
}
});
chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b == true) {
Toast.makeText(getApplicationContext(), "2 day before selected", Toast.LENGTH_LONG).show();
}
}
});
chk3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b == true) {
Toast.makeText(getApplicationContext(), "7 day before Selected", Toast.LENGTH_LONG).show();
}
}
});
}
}
Update:----
just Check condition that Your checkbox is selected or not... if check box is selected then pass the method you want pass...
for example
chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b == true) {
request=2;
MOevent.setTypeId(request);
}
}
});
As you have coded for clicking on individual section, you can also get id inside this by using following code and also put it in your database.
#Override
public void onClick(View v) {
int checkBoxId = v.getId();
}
In my program I want to be able to hide the edit text when a radio button is check and then reappear when the user clicks the other radio button.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waist2height); {
final EditText h = (EditText)findViewById(R.id.editText2);
final RadioButton rCM = (RadioButton) findViewById(R.id.radioCM);
final RadioButton rFT = (RadioButton) findViewById(R.id.radioFT);
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}}
The code below is where i have the problem i only added in the relevant part of my code instead of the entire thing
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
Unfortunately I can't seem to get it to work.
Am I missing anything? Or have I got it all wrong altogether?
I have tried h.setVisibility(View.GONE); however it just ruines the format of the xml.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==0){
h.setVisibility(View.VISIBLE);
}
else if(checkedId==1){
h.setVisibility(View.INVISIBLE);
}
}
});
Try this :
rCM.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
h.setVisibility(View.VISIBLE);
}
});
rFT.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
h.setVisibility(View.INVISIBLE);
}
});
I've made a sample and it works for me just take a look at this code :
I've declared those variables as global
RadioButton rb1,rb2;
TextView tbHideOrNot;
Then in my onCreate() I've got this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1 = (RadioButton)findViewById(R.id.rb1);
rb2 = (RadioButton)findViewById(R.id.rb2);
tbHideOrNot = (TextView)findViewById(R.id.textView);
rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.INVISIBLE);
rb2.setChecked(false);
}
}
});
rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tbHideOrNot.setVisibility(View.VISIBLE);
rb1.setChecked(false);
}
}
});
}
In my opinion, that's not a good idea use a RadioButton for the use that you want, I recommend you to use CheckBox cause you can only click ONE and this you have to check if the first RadioButton is clicked and you press the second one you'll have to uncheck the first, etc... well It's your code I'm only giving to you an advice, this code is working for me, let me know if it works for you :)
By the way if you want to use this code :
if(rCM.isChecked()){
h.setVisibility(View.VISIBLE);
}
else if(rFT.isChecked()){
h.setVisibility(View.INVISIBLE);
}
It's fine but you only will hide or not the TextView when you start the Activity and you'll have to put any RadioButton (if you want) android:checked="true" or false if you want to be showed or not the TextView, but with this code you won't get the click event on the RadioButtons.
final ImageView patientAllergyImage = (ImageView) findViewById(R.id.image);
patientAllergyImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
patientAllergyImage.setImageDrawable(getResources().
getDrawable(R.drawable.nav_down_green));
List.setVisibility(View.GONE);
}
});
I am making my List to Hide, but how do i show it when i click on the same button. I am not able to keep a boolean to check whether its clicked or not as it saying... The final local variable clicked cannot be assigned, since it is defined in an enclosing type neither an non final variable
Try this,
public void onClick(View V){
patientAllergyImage.setImageDrawable(getResources().
getDrawable(R.drawable.nav_down_green));
List.setVisibility(List.isShown() ? View.GONE : View.VISIBLE);
}
Instead of typical button, you can use a toogle Button to achieve this
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// hide the listview
} else {
// show the listview
}
}
});
Try something like this:
public void onClick(View v) {
// TODO Auto-generated method stub
patientAllergyImage.setImageDrawable(getResources().
getDrawable(R.drawable.nav_down_green));
if(List.getVisibility()==View.VISIBLE){
List.setVisibility(View.INVISIBLE)
}else{
List.setVisibility(View.VISIBLE)
}
}
Replace INVISIBLE by GONE if needed. Hope this helped.
Remove final for boolean variable or
try this
try this
if(List.getVisibility()==View.GONE)
{
List.setVisibility(View.VISIBLE);
}
if(List.getVisibility()==View.VISIBLE)
{
List.setVisibility(View.GONE);
}
Seems that there are a few issues here.
First of all you shouldn't call your listview "List" this is masking the real class called List.
Best to use "listView" with a lowercase "l" if you are stuck for a decent variable name.
You don't need to use final everywhere.
Use setImageResource instead to keep you code clean and readable.
Use the ?true:false syntax when it is readable
ImageView patientAllergyImage = (ImageView) findViewById(R.id.image);
patientAllergyImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//see if the list view is visible
bool bVisible = listView.getVisibility();
//select the image resource
int iImageRes = bVisible?R.drawable.nav_down_green:R.drawable.nav_up_green;
//Toggle Image
(ImageView)v.setImageResource(iImageRes);
//Toggle List Visibility
listView.setVisibility(bVisible?View.GONE:View.VISIBLE);
}
});