I'm creating a ListView with a custom Adapter for displaying my entries. Each row contains a checkbox, and my adapter contains the following code:
CheckBox cb = (CheckBox) v.findViewById(R.id.item_sold);
cb.setChecked(p.isSold);
setupCheckboxListener(cb, v, position);
...
private void setupCheckboxListener(CheckBox check, final View v, final int position) {
check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
System.out.println("Should become false!");
cb.setChecked(false);
} else {
System.out.println("Should become true!");
cb.setChecked(true);
}
}
});
My row XML file includes the following:
<CheckBox android:id="#+id/item_sold"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:layout_gravity="center"
android:gravity="center"
android:focusable="false"
android:clickable="false"/>
But anytime I press one of the checkboxes, check.isChecked() returns true, even if the box is unchecked. I even checked to make sure that the checkboxes were distinct and weren't picking up just the last value/etc.
Setting up the listeners inline instead of in a method doesn't seem to help, either. It's literally just the isChecked() condition that isn't working appropriately - it seems to always give me the inverse value.
Setting an onClick on the row is not acceptable in this case because I need to allow row selection for something else.
What could be causing this issue?
Related
I want to check if my Checkboxes are checked to change some values.
I got the answer for radioButton which is inserted to a RadioGroup and we can simply use :
radioGroup.setOnCheckedChangeListener(new new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{ . . . }
}
i'm looking for something like this ,However this approach is differ from Checkboxes because there is no widget like CheckboxGroup to use setOnCheckedChangeListener On it .
For example i have checkbox1 and checkbox2. how can i get as long as these two checkboxes are checked to changing some value like :
if(checkbox1==isChecked && checkbox2==isChecked)
//Do sth ...
else
//Do sth Different
EDIT : Maybe i could not explain my problem perfectly , this is just like question below but for Checkboxes
How to check state Changing of Radio Button android?
As commented you can use checkboxRef.isChecked()
if(checkbox1.isChecked() && checkbox2.isChecked()){
}
else{
}
but I want to use them in a different function , may globally access
to this checkboxes
You can use sharedPreference and with a utility class to store the state of your checkbox.
SharedPreferences helper class
This is way to check which checkbox is checked..
if(checkbox1.isChecked())
{
//means checkbox1 is checked...
}else
{
//means checkbox2 is checked...
}
If you want to set an event listener to detect when a change is made you can use:
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
//Code here
} } );
On the other hand if you want to check if it is checked you can use:
if(checkbox.isChecked()){
//Code here
}
Finally i got the answer , first of all I need to set onclick attribute as the same for all of the checkboxes , and check my Needed for approach desire output in the onClick Function,
Xml:
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="func"
android:text="1"
android:id="#+id/c1"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"
android:onClick="func"
android:id="#+id/c2"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"
android:onClick="func"
android:id="#+id/c3"/>
JavaCode:
public void func(View view) {
CheckBox checkBox1 = (CheckBox)findViewById(R.id.c1);
CheckBox checkBox2 = (CheckBox)findViewById(R.id.c2);
CheckBox checkBox3 = (CheckBox)findViewById(R.id.c3);
if(checkBox1.isChecked() && checkBox2.isChecked() && (!checkBox3.isChecked()))
{
Next = true;
}
else
{
Next = false;
}
}
checkBox1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkBox1.isChecked() && checkBox2.isChecked()){
}
}
});
I am having a problem with my listview ( 2 textview and 1 radio button).
The problem:
My idea is that the user clicks on the item in the listview and the radio button gets checked automatically.
I have been searching for a while, but I can't get the radio button to work.
My XML
<RadioButton
android:id="#+id/rdBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
My Adapter
r = (RadioButton) convertView.findViewById(R.id.rdBtn);
r.setChecked(selectedPosition == position);
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//r.setChecked(true);
Toast.makeText(context, InformationActivity.result,
Toast.LENGTH_SHORT).show();
selectedPosition = (Integer) view.getTag();
notifyDataSetInvalidated();
}
});
return convertView;
I tried
r.setChecked(true);
inside my activity class and the first click worked, but the second chooses a different item on the listview.
I hope some of you can help me.
Thanks
I found the solution here:
How to check checkbox on clicking an image?
searchList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
LinearLayout item_view = (LinearLayout) view;
final RadioButton itemcheck = (RadioButton) item_view
.findViewById(R.id.rdBtn);
if (itemcheck.isChecked()) {
itemcheck.setChecked(true);
} else {
itemcheck.setChecked(false);
}
itemcheck.setChecked(true);
<RadioButton
android:id="#+id/rdBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
I'm not sure I understand what you are trying to achieve, but maybe this would help?
r.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// radio button is checked
} else {
// radio button is not checked
}
}
});
Edit.
Assuming list is your ListView, you could do the following:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > parent, View view, int position, long id) {
r.setChecked(true); // true to make r checked, false otherwise.
}
});
you can set the list setOnItemClickListener.
You should set OnClick Listener for 'convertview' and toggle radioButton as follows-
convertview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
radioButton.toggle()
}
});
I solved this issue by using below functionality,
1.Use radiogroup.clearCheck();
To clear the all the checked state of radio buttons in listview scrolling.
2.Maintain the checked state of each radio button in a list.
If radio button checked state is changed update the value in list.
You may use checktextview inside the listview and set the type of checktextview to look like radiobutton or use background image for checktextview check so that it look like radio button.Hope this helps you
In my xml:
<CheckBox android:id="#+id/checkboxUpdateLessonPlanAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chkLessonPlanAll"
android:onClick="onCheckboxClicked"/>
In my java:
public void onCheckboxClicked(View view) {
//CheckBox box = (CheckBox) view;
CheckBox box = (CheckBox) findViewById(R.id.checkboxUpdateLessonPlanAll);
box.setChecked(!box.isChecked());
Log.v("qwerty", "checkbox clicked " + box.isChecked() + "!!");
}
I can see my log message in LogCat and it shows it as false when I click on the checkbox but its state doesn't change. It remains unchecked.
Why would you try to overwrite the default behavior with something like the default behavior? The checkbox toggles automatically on every click.
If you want to react on that, use the OnCheckedChangeListener.
To Make CheckBox checked or unchecked you can also use like
box.setChecked(true);
box.setChecked(false);
and to get state of CheckBox
if(box.isChecked()) {
//do something here...
} else {
//do something here...
}
I am working on an android project with many activities and classes.
In few of the Layouts, I have a facility in which a user can select mark all so that all the checkboxes get selected.
But, the issue I am facing is I know only the one way (which is too lengthy) to do this - By creating a markall layout for each of those existing layouts, pasting the original layout code in the markall layout code and then giving all checkboxes as checked.
This requires me to create xmls for all unmarkings also for each layout.
Please tell me a way in which I can create one xml for markall which has all checked checkboxes, and then call that layout on top of original layout when mark all is clicked.
Lets assume you have following Layout
<LinearLayout
android:id="#+id/checkbox_container"
...params>
<CheckBox ...params/>
<TextView ...params/>
<CheckBox ...params/>
<SomeView ...params/>
<CheckBox ...params/>
</LinearLayout>
get your checkBoxLayout like this inside Activity
LinearLayout container = (LinearLayout)findViewById(R.id.checkbox_container);
Iterate over the childs inside the container like this:
for (int i = 0; i < container.getChildCount(); i++){
View v = container.getChildAt(i);
if (v instanceof CheckBox){
CheckBox cb = (CheckBox)v;
// isChecked should be a boolean indicating if every Checkbox should be checked or not
cb.setChecked(isChecked)
}
}
CheckBox MarkAllCheckBox =
(CheckBox) findViewById( R.id.MarkAllCheckBox);
MarkAllCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
checkBox1.setChecked("true");
checkBox2.setChecked("true");
checkBox3.setChecked("true");
//so on
}
}
});
I'm facing a problem while trying to create Checkboxes Dynamic in my application. The design works fine, and I'm able to create as many Checkboxes as i want. The Checkboxes are put into a TableRow together with a TextView so that the text is at left side of the checkbox. But my problem is, that in my activity i can get the "status" of the checkbox, whether it's checked or not.
I use the inflater to create my Checkboxes. The xml for the checkboxes:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/TableRow" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:paddingLeft="20dip" android:id="#+id/tv_effect" android:gravity="left" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textColor="#000" android:textSize="18dip" android:layout_width="wrap_content"></TextView>
<CheckBox android:id="#+id/cb_effect" android:layout_height="wrap_content" android:text="" android:layout_gravity="right" android:layout_width="wrap_content"></CheckBox>
The function i call to create a new tablerow containing a textview and a checkbox:
public void layoutMakeSpeakerEffect(String effectName,int effectNumber)
{
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myViewSp = linflater.inflate(R.layout.speaker_settings, null);
final TextView tv_effect = (TextView) myViewSp.findViewById(R.id.tv_effect);
tv_effect.setId(effectNumber);
tv_effect.setText(effectName);
final CheckBox cb_effect = (CheckBox) myViewSp.findViewById(R.id.cb_effect);
cb_effect.setId(effectNumber);
cb_effect.setText("");
cb_effect.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(cb_effect.isChecked())
{
Toast toast = Toast.makeText(getApplicationContext(), ""+tv_effect.getText()+": ON", Toast.LENGTH_SHORT);
toast.show();
}
else
{
Toast toast = Toast.makeText(getApplicationContext(), ""+tv_effect.getText()+": OFF", Toast.LENGTH_SHORT);
toast.show();
}
}
});
tl_speakerSettings.addView(myViewSp);
}
And as mentioned, the design works fine.
But how am I able to outside this function to get the "status" off the checkbox?
And i also need a function that could clear the checkboxes status, and another function that could enable and disable the checkboxes?
I can't seem to figure this problem out by myself.
My only idea, is to make something that checks the "cb_effects" ID and afterwards checks the status of the desired checkbox.
Use setOnCheckedChangeListener instead. It will fire the onCheckedChanged(CompoundButton buttonView, boolean isChecked) method, where you can now the state of the checkbox reading the isChecked variable.
You can make use of a global integer array that will contain as many elements as there are rows in table. Set the value of the element to 1 if checkbox is checked and 0 for unchecked. By finding which element is having 1 you can fing which of the checkboxes are checked. Hope this works for you.