I added two check box in the on create method
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox1.setOnCheckedChangeListener(this) ;
checkBox2.setOnCheckedChangeListener(this) ;
the main function of the check boxes that when ischeck() a picture will be added to the mainlayout and when uncheck the picture will be removed >> I used the code bellow, the first check box is working fine the second check box when I do check it shows the pics and then they I can remove them even with uncheck ... where is the wrong in my code ??
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(checkBox1.isChecked())
{
......
mapOverlays.add(custom);
}
else {
mapOverlays.remove(custom) ;
}
if (checkBox2.isChecked())
{
....
mapOverlays.add(custom2);
}
else
{
mapOverlays.remove(custom2) ;
}
}
}
You are handling second checkbox checking differently. May be the code should look like this?
if (checkBox2.isChecked())
{
...
mapOverlays.add(custom2);
}
else
{
mapOverlays.remove(custom2);
}
Upd: if your code looks like in the current edit, then issue is the declaring custom2 variable in the if block. You are deleting not added mapOverlay, but another one declared somewhere else.
Just replace
if (checkBox2.isChecked())
{
MapItemizedOverlay custom2 = ...
by
if (checkBox2.isChecked())
{
custom2 = ...
Upd2: there is yet another issue with your onCheckedChanged() method. First if-else runs not only on checkBox1 check/uncheck but also on checkBox2 check/uncheck. Same for the second if-else.
Try to rewrite method:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.equals(checkBox1)) {
// first if-else
} else if (buttonView.equals(checkBox2)) {
// second if-else
}
}
or even better:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == R.id.checkBox1) {
if (isChecked) {
...
mapOverlays.add(custom);
} else {
mapOverlays.remove(custom);
}
} else if (buttonView.getId() == R.id.checkBox2) {
if (isChecked) {
...
mapOverlays.add(custom2);
} else {
mapOverlays.remove(custom2);
}
}
}
You need to add checkedchangelistener to checkbox 2 as well.
checkBox2.setOnCheckedChangeListener(this) ;
Related
I have been researching this, and I can't find a solution with a get drawable with a checkbox. What I'm trying to do is when you click this particular checkbox, it will filter the checkboxes of the corresponding color. This is in a fragment by the way. So, I have checkboxes that can be red, yellow, or green. If the user clicks on them, it changes the color. For example, I have a checkbox for the yellow which is chkProgress, and when it is clicked, I want to find all the checkboxes that are yellow and filter the checkboxes that are yellow. So, I have drawable ids, and I need to get the drawables to be able to locate the checkboxes that are that color. I want to be able to filter the checkboxes that are yellow when the user clicks chkProgress. So, here is my code:
private CheckBox [] checkBoxes = new CheckBox[2];
checkBoxes[0] = chkStart;
checkBoxes[1] = chkDownload;
private Drawable yellowDrawable = getResources().getDrawable(R.drawable.custom_yellow_checkbox);
chkInProgess.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chkInProgess.isChecked()){
//something here
if (chkStart.getDrawable == yellowDrawable) // need proper code here or a for loop to do this efficiently.
} else if (!chkInProgess.isChecked()){
chkInProgess.setChecked(true);
}
}
});
Here are the checkboxes that I would need to find if they are yellow:
chkStart.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
countStart = checked(chkStart, countStart);
editor.putInt(CHECK_START_ID, countStart);
editor.apply();
}
});
chkDownload.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
countDownload = checked(chkDownload, countDownload);
editor.putInt(CHECK_DOWNLOAD_ID, countDownload);
editor.apply();
}
});
Here is the method for checking the checkboxes if they are a certain color:
public int checked(CheckBox checkBox, int count){
if (checkBox.isChecked()){
count++;
} else if (!checkBox.isChecked()){
checkBox.setChecked(true);
count++;
}
if (count == 1){
checkBox.setButtonDrawable(R.drawable.custom_yellow_checkbox);
}
if (count == 2){
checkBox.setButtonDrawable(R.drawable.custom_green_checkbox);
}
if (count > 2){
count = 0;
checkBox.setButtonDrawable(R.drawable.custom_red_checkbox);
}
return count;
}
Here is the method when I resume the app:
#Override
public void onResume() {
super.onResume();
start = mSharedPreferences.getInt(CHECK_START_ID, 0);
start = idChecked(chkStart, start);
countStart = start;
download = mSharedPreferences.getInt(CHECK_DOWNLOAD_ID, 0);
download = idChecked(chkDownload, download);
countDownload = download;
}
So, I am trying to get the drawables and match them to the checkboxes that are that color. I can't find anything that would help me. Any help would be much appreciated. Thanks!
I figured out the problem. So, I made a method to check if the count of the checkbox was a certain number like I did for the checked method. I would set the checkbox with that number to visible or not.
public int colorCheckYellow(CheckBox checkbox, int value){
if (value == 1)
checkbox.setVisibility(View.VISIBLE);
else
checkbox.setVisibility(View.GONE);
return value;
}
Then I call it like this:
chkInProgess.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chkInProgess.isChecked()) {
countStart = colorCheckYellow(chkStart, countStart);
countDownload = colorCheckYellow(chkDownload, countDownload);
}else if (!chkInProgess.isChecked()) {
checkboxVisible();
}
}
I have some other code as well to make everything work but that was it!
I am having a list view (say 10 items with check boxes), I want to restrict the user to select i items (i.e.,i=1 or 2 or 3 or 4...).
Say if i=3, the user can only select 3 check boxes from the list, he will be not able to select the 4th check box. And if we scroll the list view the items will get reset to the old values as we know that can be handle by using Boolean state in the model class.
How can I restrict the user to select particular items from the list view?
Here is my listener:
final int i=3;
holder.cb_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
model.setChecked(isChecked);
if (isChecked) {
GlobalCounter++;
} else {
GlobalCounter--;
}
if (GlobalCounter>i) {
buttonView.setChecked(false);
model.setChecked(false);
}
}
});
You can do it by using
boolean checked = ((CheckBox) view).isChecked();
if(!checked){
//perform action here if needed
}
but in your case you have to manage no of checked boxes 3 information
Do this
final int i=3;
holder.cb_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (GlobalCounter>i) {
buttonView.setChecked(false);
return;
}
model.setChecked(isChecked);
if (isChecked) {
GlobalCounter++;
} else {
GlobalCounter--;
}
}
});
Here is my answer, for those who want to add particular selections in the list view.
Inside Adapter class on onClickListener:
holder.cb_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
model.setChecked(isChecked);
if (isChecked) {
count++;
} else {
count--;
}
Log.d("ttt", "c="+count+" mc="+max_count);
if (count>max_count) {
buttonView.setChecked(false);
model.setChecked(false);
count=max_count;//count will not exceed the limit.
Toast.makeText(context, "You can only select "+max_count, Toast.LENGTH_SHORT).show();
}
}
});
Initialize the int count=0,max_count=0; global to the adapter class, while setting the adapter pass the max_selection count to the adapter class.
Here it is for the value 3:
final ComboAdapter adapter=new ComboAdapter(getApplicationContext(), R.layout.custom_items, arr_list,3);
lv_combo.setAdapter(adapter);
I want to inspect something before CheckBox's state changed. If user has logged in, change the state and post data to server. Otherwise, keep the state and show a log-in activity.
OnCheckedChangeListener does not fit. Is there anything like PreferenceChangeListener (return false to keep stateļ¼ in CheckBox.
Or other methods.
Thank you!
Update:
OnClickListener work for me.
#Override
public void onClick(View view) {
boolean isChecked = checkBox.isChecked();
checkBox.setChecked(!isChecked);
if (loggedIn) {
checkBox.setChecked(isChecked);
// push data to server
} else {
// show log-in activity
}
}
If I'm not misunderstood your question, you can still achieve it by using OnCheckedChangeListener:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isLogin) {
buttonView.setChecked(true);
Toast.makeText(this, "Sending data", Toast.LENGTH_SHORT).show();
} else {
buttonView.setChecked(false);
Toast.makeText(this, "Show login activity", Toast.LENGTH_SHORT)
.show();
}
}
You can do something like this:
CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
or if you just want to see if the checkbox has been checked or not, then you can use this
CheckBox checkBox = (CheckBox)v.findViewById(R.id.Checkbox);
checkBox.setChecked(!checkBox.isChecked());
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// awesome code you may use
// if (isChecked)
// (buttonView.getId() == R.id.check1 ? check2 : check1)
// .setChecked(false);
//Or use the code below
if (isChecked) {
switch (buttonView.getId()) {
case R.id.check1:
check2.setChecked(false);
Toast.makeText(getApplicationContext(), "Check1 is selected!!! =)",
Toast.LENGTH_LONG).show();
break;
case R.id.check2:
check1.setChecked(false);
Toast.makeText(getApplicationContext(), "Check2 is selected!!! =)",
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
I have 3 checkboxes and 2 edittexts. When the user checks one checkbox and enters data in one edittext calculations take place. If i check another checkbox the edittext values should automatically clear. However the data in the edittexts exist even after i check other checkboxes.I tried finish() which shuts the application. Any idea how can this be achieved without closing the app?
Sorry if this question sounds weird. Iam just learning android
Thank you.
code for my onCheckedChangedListener
public void onCheckedChanged(CompoundButton predictionChkView, boolean isPredictionChecked)
{
// TODO Auto-generated method stub
switch(predictionChkView.getId())
{
case R.id.chkLastMileage1:
isChkLastMileage1=true;
chkLastMileage5.setChecked(false);
chkLastMileage10.setChecked(false);
ETPredictKm.setText("");
ETPredictFuelQty.setText("");
break;
case R.id.chkLastMileage5:
isChkLastMileage5=true;
chkLastMileage1.setChecked(false);
chkLastMileage10.setChecked(false);
ETPredictKm.setText("");
ETPredictFuelQty.setText("");
break;
case R.id.chkLastMileage10:
isChkLastMileage10 =true;
chkLastMileage1.setChecked(false);
chkLastMileage5.setChecked(false);
ETPredictKm.setText("");
ETPredictFuelQty.setText("");
break;
}
}
Code for the onFocusChangedListener
public void onFocusChange(View predictionFocusView, boolean hasPredictionETFocus)
{
// TODO Auto-generated method stub
FuelStoredInfo predictInfo = new FuelStoredInfo(this);
predictInfo.open();
predictInfo.getAvgMileage(this);
predictInfo.close();
try
{
predictKm = Long.parseLong(ETPredictKm.getText().toString());
predictFuetlQty = Double.parseDouble(ETPredictFuelQty.getText().toString());
}
catch(NumberFormatException ne)
{
ne.printStackTrace();
}
if(isChkLastMileage1 ==true || isChkLastMileage5==true||isChkLastMileage10==true)
{
if(ETPredictKm.hasFocus())
{
ETPredictKm.setText("");
if(predictFuetlQty!=0)
{
predictKm =(long) (predictionMileage*predictFuetlQty);
//setPredictKm(predictKm);
ETPredictKm.setText(String.valueOf(predictKm));
}
}
else if(ETPredictFuelQty.hasFocus())
{
ETPredictFuelQty.setText("");
if(predictKm!=0)
{
predictFuetlQty =predictKm/predictionMileage;
//setPredictFuetlQty(predictFuetlQty);
ETPredictFuelQty.setText(new DecimalFormat("##.##").format(predictFuetlQty));
}
}
}
else
{
Toast.makeText(getApplicationContext(), "Please check a checkbox!", Toast.LENGTH_LONG).show();
}
}
public void onClick(View v)
{
// TODO Auto-generated method stub
ETPredictKm.setText("");
ETPredictFuelQty.setText("");
chkLastMileage1.setChecked(false);
chkLastMileage5.setChecked(false);
chkLastMileage10.setChecked(false);
}
Add to your function.
EditText editText = (EditText)findViewById(R.id.edittext);
editText.setText("");
If you store a reference to your editText you can just call
editText.setText("");
Do this inside your onCheckedChange listener when the checkbox is in your desired state.
Add a checkbox oncheckedchangelistener and then put edittest.setText(""); inside.
Do something like this:
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
edittext.setText("");
}
});
Also, don't forget to declare variables for your editexts, checkboxes, etc like this:
//example
final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
Let me know if it works :)
Alternative:
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( checkbox.isChecked() )
{
edittext.setText("");
}
}
});
When you check the other checkbox do this
edittext.setText("");
for other EditText
Edit -----
Add FocusChangeListener for all of your EditText and do this ----
ETPredictKm.setOnFocusChangeListener(new OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
ETPredictKm.setText("");
}
});
CheckBox check1 = (CheckBox) findViewById(R.id.checkbox1);
Checkbox check2 = (Checkbox) findViewById(R.id.checkbox2);
if (check1.isChecked()) {
EditText myEditText = (EditText) findViewById(R.id.edit_text_1);
myEditText.setText("");
}
if (check2.isChecked()) {
EditText myEditText2 = (EditText) findViewById(R.id.edit_text_2);
myEditText1.setText("");
}
I'm creating a Android application which uses a Switch.
I'm trying to listen for changes and get the value when changed.
I have two questions when using switches:
What action listener do I use?
How do I get the the switch value?
Switch s = (Switch) findViewById(R.id.SwitchID);
if (s != null) {
s.setOnCheckedChangeListener(this);
}
/* ... */
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(this, "The Switch is " + (isChecked ? "on" : "off"),
Toast.LENGTH_SHORT).show();
if(isChecked) {
//do stuff when Switch is ON
} else {
//do stuff when Switch if OFF
}
}
Hint: isChecked is the new switch value [true or false] not the old one.
Since it extends from CompoundButton (docs), you can use setOnCheckedChangeListener() to listen for changes; use isChecked() to get the current state of the button.
Switch switch = (Switch) findViewById(R.id.Switch2);
switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
...switch on..
} else {
...switch off..
}
}
});
i hope this will solve your problem
I added this in kotlin
switchImage.setOnCheckedChangeListener { compoundButton: CompoundButton, b: Boolean ->
if (b) // Do something
else // Do something
}
Kotlin but in More readable Java Style
videoLoopSwitch.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
override fun onCheckedChanged(switch: CompoundButton?, isChecked: Boolean) {
videoPlayer?.apply {
setLooping(isChecked)
}
}
})
Try This one
#OnClick({R.id.sw_auto_reload})
void onChangedAutoReload(){
Switch s = (Switch) findViewById(R.id.sw_auto_reload);
if (s != null) {
s.setOnCheckedChangeListener(this);
s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
paymentSettingsPresenter.onAutoReload(b);
}
});
}
}