check box in expandable listview - android

I have an expandable listview and I have check box for both parent and child. The requirement is that if I check the checkbox of parent , then the check box of children should also get selected.Here is the code
holder.editCheck
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
COuld any one help me with this

Choose the one you want:
The first one will apply the state of the parent checkbox to the child one.
parent_checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
children_checkbox.setChecked( parent_checkbox.isChecked() );
}
});
Or:
The second will only apply the state if the is checked. Ie. if the parent is unchecked, child keep its previous state.
parent_checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (parent_checkbo.isChecked()) children_checkbox.setChecked( true );
}
});

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
}
}
});

how to check and uncheck single radio button

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);

Check box inside a ListView, setChecked(true) is not working in Nougat

I have ListView of each item with text view and a check box
when clicking on each item I'm changing the check box state true/false to display selected items in a list.
listSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Helper.hideKeyBoard(getApplicationContext(), view);
checkBox = (CheckBox) view.findViewById(R.id.chkbox_value);
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}
else{
checkBox.setChecked(true);
}
}
});
I tried using runaable also :
checkBox.post(new Runnable() {
#Override
public void run() {
checkBox.setChecked(true);
}
});
it is working on Marshmallow correctly but not working on Nougat!
Note : when I click on the second item the first item tick is showing in nougat.
Inorder to display list of selected items u need to maintain checkbox state
I have found solution to this issue if you are still facing :
Replace your below code :
checkBox.post(new Runnable() {
#Override
public void run() {
checkBox.setChecked(true);
}
});
To my code & see if it works:
checkBox.post(new Runnable() {
#Override
public void run() {
checkBox.setChecked(true);
checkBox.jumpDrawablesToCurrentState();
}
});
The flow of your code is going like this:
You are clicking a checkbox -> it detects it is clicked and it check itself -> You are checking is it is checked or not -> If its checked then you are using setChecked(false) which unchecks your checkbox.
Remove following code and it should work:
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}
else{
checkBox.setChecked(true);
}
Use this listener to check if a checkbox is selected or not and do an action correspondingly.
checkBox setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
}
}
);

Delete the row as we click the checkbox

I have a strange requirement. I have multiple rows having checkbox each. Now i need to delete the row as soon as the check box is checked. I have done this so far but its not working.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CheckBox checkbox = new CheckBox(this);
checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
delete();
}
});
}
Thanks In Advance ...
This is not a good design. The user does not expect a checkbox to delete an item from the list. However, if you really want to do it, use an OnCheckedChangeListener:
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChange(CompoundButton checkbox, boolean checked) {
delete();
}
}

How to make only one widget focusable in a custom listview?

I have a custom listview with one checkbox and two textviews. I'm using a custom adapter to present the list view. The problem is that the onItemclick function doesn't work for the checkbox if I focus on the checkbox directly, it works only when the list is focused first and then checkbox.I want my listview to work similar to checkedtextview where the checkbox gains focus by default.Can anybody suggest me a workaround for this problem? Thank you.
Try to add a click listener to your view returned with the getView() method as follows:
view.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
/// checkboxview.clearFocus()
/// checkboxview.requestFocus()
}....
EDIT. onclicklistener for checkbox:
CheckBox chckKill = new CheckBox(context);
chckKill.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
///// your code here
}
}
});
Add this code to list view activity and change according to your specification.
View v;
LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.listview, null);
CheckBox checkBox = (CheckBox)v.findViewById(R.id.check_box);
checkBox.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
//Action
}
});

Categories

Resources