I have a screen that add dynamically a row with two textView and one CheckBox. My problem is that I only want that the user could check one, and the others have to be uncheck. If the user push another checkbox, enable this and disable all the other checkbox. I've test some things but I don't get the functionality that I want.
I can't change the configuration from checkbox to radioButton and I have to use the method that I'm using:
private selected_position = -1;
protected void addGreetingToListView(final GreetingListJson greetings) {
View row = inflater.inflate(R.layout.main_greetings_settings_row, greetingListView, false);
row.setTag(greetings);
playButton = (ToggleButton) row.findViewById(R.id.detail_play_greeting);
TextView greetingName = (TextView)row.findViewById(R.id.greetingTitle);
greetingName.setDuplicateParentStateEnabled(true);
TextView greetingDescription = (TextView)row.findViewById(R.id.greetingDescription);
greetingDescription.setDuplicateParentStateEnabled(true);
checkBox = (CheckBox) row.findViewById(R.id.checkBox_greeting_list);
checkBox.setTag(greetings);
checkBox.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
if (((CheckBox) view).isChecked())
{
selected_position= greetingListView.getChildCount();
}
else
{
selected_position=-1;
}
}
});
greetingName.setText(greetings.Name);
greetingDescription.setText(greetings.Description);
row.setOnClickListener(this);
row.setOnLongClickListener(this);
row.setFocusable(true);
greetingListView.addView(row);
}
The XML of the Checkbox:
<CheckBox
android:id="#+id/checkBox_greeting_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"/>
Firstly, declare an ArrayList in the class:
ArrayList<CheckBox> mCheckBoxes = new ArrayList<CheckBox>();
Then in addGreetingToListView add every new checkbox to mCheckBoxes and modify the click listener of the checkbox:
checkBox.setTag(greetings);
mCheckBoxes.add(checkBox);
checkBox.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
if (((CheckBox) view).isChecked())
{
for (int i = 0; i < mCheckBoxes.size(); i++) {
if (mCheckBoxes.get(i) == view)
selected_position = i;
else
mCheckBoxes.get(i).setChecked(false);
}
}
else
{
selected_position=-1;
}
}
});
Related
I'am having Custom dialog in which i've displayed a listView with checkboxes.I want to select all checkBoxes of ListView by Clicking button in dialog.
here is my button onClickListener
selectAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i = 0; i < listView.getChildCount(); i++) {
LinearLayout myLayout =(LinearLayout)listView.getChildAt(i);
CheckBox cb = (CheckBox) myLayout.findViewById(R.id.checkbox);
cb.setChecked(true);
}
}
});
with above code i have can only check those checkboxes which are on view.
I know this happens because listView reuses views.
please suggest me what to do
You can add boolean variable into your model class and set true on your button click listener, check this:
class YourModel{
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
Your button onClick change.
public void onClick(View view) {
for (int i = 0; i < yourArrayList.size(); i++) {
yourArrayList.get(i).setSelected(true);
}
yourAdapter.notifyDataSetChanged();
}
your adapter getView method
public View getView ( final int position, View convertView, ViewGroup parent){
CheckBox cb = (CheckBox) myLayout.findViewById(R.id.checkbox);
cb.setChecked(yourModel.isSelected());
}
my Problem is that i want to create a Checklist with multiple Checkboxes. The biggest Problem is i have more than 100 Checkboxes. I would like an CLEAR Button that clears all Checkboxes by clicking.
How can I do that? And have you an example how to solve it?
The only way i know is that:
Button clear = (Button) findViewById(R.id.clearbtn);
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox2);
cb1.setChecked(false);
}
});
But that way isnt really effective with over 100 checkboxes ...
If you are keeping all the checkboxes on single ViewGroup then it can be done by getting all the childs of that ViewGroup and unchecking. For example 'parent' is the layout that contains all the checkboxes. You can uncheck all by:
for (int i = 0; i < parent.getChildCount(); i++) {
View view = parent.getChildAt(i);
if (view instanceof CheckBox) {
((CheckBox) view).setChecked(false);
}
}
I have solve it. Thanks to #Tuby and #android_hub for the idea with getChildCount().
And special thanks to #Hammad Akram. Now it works :D. My code now:
final LinearLayout ll = (LinearLayout)findViewById(R.id.ll_a320_main);
final int ccount = ll.getChildCount();
Button clear = (Button)findViewById(R.id.btn_a320_clear);
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(ACT_A320.this, "Child: "+ccount,Toast.LENGTH_LONG).show(); -- Test for checking count of Child
for(int i=1; i<ccount;i++){
v = ll.getChildAt(i);
if(v instanceof CheckBox ){
((CheckBox) v).setChecked(false);
}
}
}
});
Now all Checkboxes would detected and set to false.
I can dynamically add the text fields in my LinearLayout container.
Now I need to collapse those added fields in click of the Button those added fields should get collapse with any label.
Can it be done? Below is the code that i can add the EditText dynamically.
txtHeading = (EditText)findViewById(R.id.heading);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
buttonAdd.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0)
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.list_view, null);
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.layout_icon_select);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
textIn.setText(null);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}
});
container.addView(addView);
}}});
btnsave = (Button) findViewById(R.id.btn_save);
btnsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//What to insert here?
on click to save button in need to collapse all the textView Elements.
let us consider Heading is the label and it is named as Gender so if i added the options as male and female then after clicking the save button this should get collapsed under the Heading.
Based on what I'm kind of guessing that you want... I would create a custom view class based on a linear layout. Each having two edit texts.
Create and add this class to dynamically when the add button is pressed. When the save button is pressed, you can loop over the parent LinearLayout's children and then call a helper method to collapse the second edit text element.
To Loop over the parent and collapse children. Now the CustomElement and the collapseOptionField() call is custom code you must write.
LinearLayout parentLayout = findViewById(...);
for ( int i = 0; i < parentLayout.getChildCount(); i++ ) {
CustomElement ce = parentLayout.getChildAt(i);
ce.collapseOption();
}
Update
Custom class with collapse-able option:
public class TextWithOption extends LinearLayout {
EditText edit0;
EditText edit1;
public TextWithOption(Context context, AttributeSet attrs) {
this(context);
}
public TextWithOption(Context context) {
super(context);
View view = LayoutInflater.from(context).inflate(R.layout.edit_text_option, null);
edit0 = (EditText) view.findViewById(R.id.edit_text0);
edit1 = (EditText) view.findViewById(R.id.edit_text1);
addView(view);
}
public void collapseOption() {
edit1.setVisibility(View.GONE);
}
public void showOption() {
edit1.setVisibility(View.VISIBLE);
}
}
Activity code that will add new options and then collapse them:
final LinearLayout optionsLayout = (LinearLayout) findViewById(R.id.outer_layout);
Button addButton = (Button) findViewById(R.id.button_add);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextWithOption to = new TextWithOption(context);
optionsLayout.addView(to);
}
});
((Button) findViewById(R.id.button_save)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for ( int i = 0; i < optionsLayout.getChildCount(); i++ ) {
((TextWithOption)optionsLayout.getChildAt(i)).collapseOption();
}
}
});
public static void collapse(final View v) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
v.startAnimation(anim);
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
v.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
onclick of button write view.collapse
view equals to whatever view you want collapse
I want to delete selected rows from a todo list by clicking on the checkbox and deleting them trough the delete button, for that I am within my custom adapter setting a setOnCheckedChangeListener on my checkbox and setOnClickListener on my delete button, now keep in mind that the delete button is inflated on my fragment view and I am using it on my row view, but the problem is only the last element from my todo list is getting deleted not the rest of them, I tried working within the fragment view and notify the adapter later on but all I got was a null pointer error on my checkbox.
Row View:
todoCheckBox = (CheckBox) convertView.findViewById(R.id.todo_CheckBox);
todoTextView.setText(values.get(position));
todoCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Toast.makeText(getContext(), " CheckBox Status: " + isChecked, Toast.LENGTH_LONG).show();
mDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mDelete.isPressed() && (todoCheckBox.isChecked())) {
//convertView.clearFocus(position);
mAdapter.clear();
//mAdapter.notifyDataSetChanged();
}
}
});
}
});
Fragment View:
#TargetApi(9) // remember this for isEmpty()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_todo, container, false);
ArrayList<String> todoList = new ArrayList<String>();
mAdapter = new UsersAdapter(getActivity(), todoList);
listViewToDo = (ListView) v.findViewById (android.R.id.list);
listViewToDo.setAdapter(mAdapter);
mToDoField = (EditText) v.findViewById(R.id.todo_editText);
mDelete = (Button) v.findViewById(R.id.delete_button);
mAdd = (Button)v.findViewById(R.id.add_button);
mAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String toDo = mToDoField.getText().toString().trim();
if (toDo.isEmpty()){
return;
}
mAdapter.add(toDo);
mToDoField.setText("");
}
});
return v;
}
}
You need to assign an onclick listener to your delete button outside of the onChecked statement. Add it in code just after you assign the onClick event to the add button. This is because a view in android can only have 1 listener per event type.
The onClick event can look something like below using a sparese
mDelete.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
SparseBooleanArray checked = listViewToDo.getCheckedItemPositions();
for (int i = 0; i < listViewToDo.getCount(); i++){
if (checked.get(i)==true)
{
mAdapter.remove(i);
}
mAdapter.notifyDataSetChanged();
}
listViewToDo.clearChoices();
}
});
I am creating checkboxes at runtime but i don't know how to find that which checkbox is checked and which is unchecked?This is the code.
for (String s : options)
{
chk = new CheckBox(this);
System.out.println(s);
chk.setId(i++);
chk.setText(s);
selected=s;
chk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked())
DisplayToast(selected);
else
DisplayToast(selected);
}
});
lm.addView(chk);
}
You can get the id of CheckBox using getId() method ..
CheckBox checkBox = ((CheckBox) v);
if (checkBox.isChecked())
{
int checkBoxId = checkBox.getId(); // It will give you checked checkbox id..
DisplayToast(selected);
}
else
DisplayToast(selected);
Update:
To get all CheckBox states you have to iterate thru ChildViews of lm layout.
Something like,
for (int i = 0; i < lm.getChildCount(); i++) {
View v = lm.getChildAt(i);
if (v instanceof CheckBox) {
if (((CheckBox) v).isChecked())
// Check Checkbox
else
// Unchecked Checkbox
}
}
Note: I would suggest you can use onCheckedChangeListener instead of View.OnClickListener() to check - uncheck event of CheckBox. (But it is optional, it also works with OnClickListener)
You should use onCheckedChangeListener to define whether your checkbox checked:
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//checked
} else {
//not checked
}
}
});