Access the button in parent view from a switch android - android

I have an issue accessing the view from the parent of a compoundbutton.
((Switch) convertView.findViewById(R.id.push_switch)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (rowItem.getPush().equals("N")) {
rowItem.setFollow(true);
rowItem.setPush("Y");
setFollowTickGreen(rowItem, convertView);
}
else if (rowItem.getPush().equals("Y")) {
rowItem.setPush("N");
}
}
});
This code shows my switch button but I need to get access to convertView to change something else when this button is pressed.
This is being done inside an adapter, so the switch code is in the getView function but the setFollowTickGreen is outside of that function.
I can't set convertView as final as it changes throughout the getView function.
I understand that the compoundButton is the switch but the switch is at the same level that I want to access a Button.
Is there a general way of doing this?
This would be similar for other situations, as well as compoundbuttons I assume.
Thanks.

Does this help..
final View tmpConvertView=convertView;
((Switch) convertView.findViewById(R.id.push_switch)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (rowItem.getPush().equals("N")) {
rowItem.setFollow(true);
rowItem.setPush("Y");
setFollowTickGreen(rowItem, tmpConvertView);
}
else if (rowItem.getPush().equals("Y")) {
rowItem.setPush("N");
}
}
});

Related

Android - how to trigger setOnCheckedChangeListener with linear layout?

In my recyclerview onBindView method, I have this piece of code:
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// execute the code inside the onCheckedChanged method
}
});
And then I also have this:
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something
}
});
I'm doing this because I need padding around the checkbox (linear layout), but I need the checkbox to stay the same during scrolling by using OnCheckedChangedListener. I need to use the boolean isChecked parameter to execute my further code.
How can I execute the code inside the onCheckedChanged by clicking on the linear layout and not on the checkbox?
If you need to change the checkbox field on clicking linear layout add this code
holder.checkBox.setChecked(!holder.checkBox.isChecked());
else add this
holder.checkBox.setChecked(holder.checkBox.isChecked());
This will call the onCheckedChanged method.
try this :
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(holder.checkBox.isChecked()){
holder.checkBox.setChecked(false);
//do your code for check status false
}
else{
holder.checkBox.setChecked(true);
//do your code for check status true
}
}
});

android checkbox and check change listener

in my android app i have a custom table list with one checkbox for each row.
i would like to check, if a checkbox was checked or unchecked.
for this i have this code:
SelectCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (SelectCheckBox.isChecked()) {
Log.e("-->", "IS CHECKED");
} else {
Log.e("-->", "IS NOT CHECKED");
}
}
});
this works fine, if i check/uncheck the checkboxes.
but i have got another code, which checks all checkboxes at once, after pressing a button.
SelectCheckBox.setChecked(true);
but than the CheckedChangeListener will not be active.
Here my Code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_CheckAll:
for (int i = 0; i < 30; i++) {
SelectCheckBox.setChecked = true;
break;
}
}
}
SelectCheckBox.performClick() insted of SelectCheckBox.setChecked(true); should solve your issue. I think the way you are coding .setChecked(true); is forcing the checkbox to stay in checked state.
Can you post your full activity code.

onCheckedChanged called automatically

I have a switch in a recyclerview and data is displayed in the recyclerview after retrieving data from DB. When the recyclerview is opened I read DB and if a field in DB is "Y" I enable the switch or else I disable the switch.
Now the problem is along with it the onCheckedchanged listener is also called, I want the onCheckedChanged to be called only when user sets the switch manually.
On opening the recyclerview below is executed:
holder.enabledisable.setChecked(messengerRecord.get_is_valid().equalsIgnoreCase("Y"));
ViewHolder class:
public class viewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener{
public SwitchCompat enabledisable;
public viewHolder(View v) {
enabledisable = (SwitchCompat) v.findViewById(R.id.enabledisable);
enabledisable.setOnCheckedChangeListener(this);
...................................
...................................
OncheckedChanged method which is called when the recyclerView is just opened:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.v("ranjith","called oncheckedchanged");
MessengerRecord rec;
rec = dbHelper.getRecord(descview.getText().toString());
switch (buttonView.getId()) {
case R.id.enabledisable:
if (isChecked) {
rec.set_is_valid("Y");
dbHelper.updateRecord(rec);
}
}
In Layout file:
<android.support.v7.widget.SwitchCompat
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:id="#+id/enabledisable"
android:layout_alignRight="#+id/textview_to"
android:layout_alignEnd="#+id/textview_to"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
It's weird all of us have this problem but not official Google answer to this simple problem.
The MOST simple it's to check:
buttonView.isPressed()
If true, means the user clicked the view.
No global variables are needed.
Try this
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isPressed()) {
... //returns true, if user clicks the switch button
}}
I've ended up using this subclass of SwitchCompat to avoid this issue. This way I don't need boilerplate code where I'm using this class.
Whenever you need to change the checked without firing the listener, use setCheckedSilent instead of setChecked:
import android.content.Context;
import android.os.Parcelable;
import android.support.v7.widget.SwitchCompat;
import android.util.AttributeSet;
/**
* Created by emanuel on 30/5/16.
*/
public class Switch extends SwitchCompat {
private OnCheckedChangeListener listener;
public Switch(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
this.listener = listener;
super.setOnCheckedChangeListener(listener);
}
#Override
public void onRestoreInstanceState(Parcelable state) {
super.setOnCheckedChangeListener(null);
super.onRestoreInstanceState(state);
super.setOnCheckedChangeListener(listener);
}
public void setCheckedSilent(boolean checked) {
super.setOnCheckedChangeListener(null);
super.setChecked(checked);
super.setOnCheckedChangeListener(listener);
}
}
onRestoreInstanceState was triggering the listening also, when set in the onViewCreated method and you are going back to a previous fragment.
Hope it works for you!
Since RecyclerView is recycling views, a previously attached OnCheckedChangeListener can be triggered when setting checked value for the Switch of the new item.
When binding new data to an item:
switch.setOnCheckedChangeListener(null) // remove any existing listener from recycled view
switch.isChecked = [true/false] // will no longer trigger any callback to listener
switch.setOnCheckedChangeListener { btnView, isChecked ->
// do exiting stuff
}
This works for me:
boolean selected = preferences.isChecked();
yourCheckBox.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView.isPressed()) {
preferences.setChecked(isChecked);
} else {
yourCheckBox.setChecked(selected);
}
}
});
Kotlin Developers:
checkboxXYZ.setOnCheckedChangeListener { btnView, isChecked ->
if (btnView.isPressed) {
}
}
use a global boolean variable and when read a data from DB set it "true" and after check(if) in onCheckChange set it "false" again. in first of onCheckChange method check if this variable is false execute codes else return(default value of this variable must be false ) ;
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!isSourceDB){
Log.v("ranjith","called oncheckedchanged");
MessengerRecord rec;
rec = dbHelper.getRecord(descview.getText().toString());
switch (buttonView.getId()) {
case R.id.enabledisable:
if (isChecked) {
rec.set_is_valid("Y");
dbHelper.updateRecord(rec);
}
}//end if
isSourceDB = false; }// end oncheckedchange
If you are changing state in adapter then
holder.rawManageDriverLayoutBinding.rawManageDriverSwitch.setOnCheckedChangeListener(null);
before setOnCheckedChangeListener.
I faced the same problem inside ViewPager screen for fragments. When we switch between fragments, onCheckedChanged Listener is called again and again.
If anyone still looking for this problem, please try it.
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isInTouchMode()) {
... //Your code will come here.
}
}
Only solution is to use the method isPressed() in the listener

Android setOnCheckedChangeListener calls again when old view comes back

I cannot solve an issue with the getGroupView-method.
the problem is that the listener setOnCheckedChangeListener is getting invoked to many times.
Let say i check a certain checkbox-item. Then I scroll it out of view and then scroll back. What happends is that the listener is called once again. And the problem is that I store checkbox-id's in an arraylist inside this listener to use it later in the code. The consequence is that more elements is added to the arraylist everytime the listener is called and distortes the data.
Is there a solution to this? what should I do? Should I for instance unregister the listener?
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = null;
final int group_position = groupPosition;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_phrase, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.groupTitle);
viewHolder.checkBox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
checked.add((Integer) viewHolder.checkBox.getTag());
}
else {
checked.remove((Integer) viewHolder.checkBox.getTag());
}
}
});
view.setTag(viewHolder);
viewHolder.checkBox.setTag(groupPosition);
} else {
view = convertView;
((ViewHolder)view.getTag()).checkBox.setTag(groupPosition);
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(titles[groupPosition]);
for (int i = 0; i < checked.size(); i++) {
if (checked.get(i) == group_position) {
holder.checkBox.setChecked(true);
}
else if (checked.get(i) != group_position) {
holder.checkBox.setChecked(false);
}
}
return view;
}
What version of Android are you using?
It seems to be an issue for multiple components, especially with a checkedChange() method (CheckBox, RadioButton) and I can't provide a good explanation why it is happening.
I would assume because it registers the change of the position state and grabs the change of other properties? A similar issue was addressed here
In any case, a workaround around this could be to add an OnClickListener().
CheckBox yourCheckBox = (CheckBox) findViewById (R.id.yourId);
yourCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
//Case 1
}
else
//case 2
}
});
OnCheckedChangeListener worked for me with one if condition
if(buttonView.isShown())
{
//ToDo code
}
Add the below to ToggleButton layout:
android:saveEnabled="false"
This would make sure android doesn't store the check state to RestoreInstance and in turn won't cause the state change experienced by you.
Set the listener to null before calling setCheck() function, and enable it after that such as the following:
checkBox.setOnCheckedChangeListener (null);
checkBox.setChecked(true);
checkBox.setOnCheckedChangeListener (this);
Reference: Change Checkbox value without triggering onCheckChanged
Add an additional if statement isShown() in the OnCheckedChange method and it solves the problem that the OnCheckedChangeListener gets called multiple times.
There is no need to change to an onClickListener:
#Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isShown()) {
if (buttonView.isChecked()) {
//Do something
} else {
//Do something
}
}
}
strong textThere is many way to solved the issue
checkBoxSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// write here your code for example ...
if(isChecked){
// do somtheing when is checked
}else{
// do somthing when is removed the check**strong text**
}
}
});
**and there is another way **
checkBoxSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
//do something
}else{
//do something
}
});
I faced the same problem and struggled for several hours seeing all discussions related to this. I tried to fix this by keeping
viewHolder.checkBox.setTag(groupPosition);
this statement before viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() this listener. And the code works exactly as expected.
I was stuck with the same issue and found a way around by using on click listener
First check if the checkbox has the onclicklistener assigned to it or not. If not that means the adapter is setting the value for the first.
if(!holder.checkbox.hasOnClickListeners())
{
holder.checkbox.setChecked(data.get( position ).getSelected());
}
Now after checking this
holder.checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
data.get( position ).setSelected(holder.checkbox.isChecked());
holder.checkbox.setChecked(data.get( position ).getSelected());
Toast.makeText(getApplicationContext(),
data.get( position ).get_number()+" : "+position, Toast.LENGTH_LONG).show();
}
});
Hopefully the scrolling issue goes away. Works fine here.

How to implement a CheckBox's onTouchEvent

How do you implement this onTouchEvent? It should fire when the user checks or unchecks the CheckBox widget.
CheckBox checkBox = new CheckBox(activity);
checkBox.setText("Don't present me information again.");
checkBox.onTouchEvent(.....);
The CheckBox widget (and any other widget that extends CompoundButton) has a method setOnCheckedChangeListener, which is the bit you're lacking (you probably don't want to use onTouchEvent in this case).
This example should replace the final line of code in your snippet:
checkBox.setOnCheckedChangeListener( new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if ( isChecked ) {
// do some stuff
}
}
});
what you need in your case is this
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
Toast.makeText(yourActivity.this,"CheckBox is Checked ..",3000).show();
}
else{
Toast.makeText(yourActivity.this,"CheckBox is Unchecked ..",3000).show();
}
}
});

Categories

Resources