0

I have a toggle set in my java code for a button in xml. The code is as follows:

@Override
    public void onClick(final View view) {
        switch (view.getId()) {
        case R.id.selectable_text:
            if(view instanceof CheckedTextView){
                CategoryCheckableRow checkableRow = ((CheckedTextView)view).getCategoryCheckableRow();
                toggleCategoryRow(view, checkableRow);
                Log.d("newsdash","I am toggling in dash onclick");
                if (!mCategoriesSet.add(checkableRow)) {
                    mCategoriesSet.remove(checkableRow);
                }
                mDoneButton.setEnabled(!mCategoriesSet.isEmpty());
            }
            return;
        case R.id.button_done:
            sendCategoriesToActivity();
            ((DashboardManageNewsCategoriesActivity) getActivity()).updateCategoriesMap(mCategoriesSet);
            break;
        default:
        }

}

I am planning to uncheck all other "checks" when my currect checkbox is selected. say i have checkboxes 1 ,2 and 3, if I check 1 I want 2 and 3 to be unchecked and so on. Is there a way I can achieve this with my above code? Say (if cursor ( or possibly view). getcursor or currentposition ) = current checkbox selected ) { uncheck all other checkboxes except current one} ?

Also here's the toggle row:

private void toggleCategoryRow(final View view, final CategoryCheckableRow checkableRow) {
        final CheckedTextView textView = (CheckedTextView) view;
        textView.toggle();
        checkableRow.setSelected(textView.isChecked());
    }

Here;s the corresponding xml file (for reference):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/altercolor2"
    android:orientation="vertical">
  <ScrollView 
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/altercolor2"
        android:orientation="vertical" >



        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <Button
                android:id="@+id/button_top_news"
                android:layout_width="match_parent"
                android:layout_height="@dimen/manage_news_btn_ht"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:background="@drawable/manage_market_category_btnbg"
                android:gravity="left|center_vertical"
                android:paddingLeft="10dp"
                android:drawablePadding="10dp"
                android:text="@string/button_top_news"
                android:textColor="#cccccc"
                android:textSize="@dimen/title" 
                android:drawableLeft="@drawable/arrow_expand_collapse"/>




        </RelativeLayout>
</linearlayout>
</,... etc
  • 3
    Is there a reason you aren't using `RadioGroup` since that is the expected functionality of it and not of a `Checkbox`? – codeMagic Apr 09 '14 at 17:55
  • the reason why i m not using radiogroup is i have separate categories of things with their own subcategories, so i m finding it hard to maintain besides i have never used them before –  Apr 09 '14 at 17:59
  • It would be a good time to start. When a user sees checkboxes, typically they expect to be able to check multiple boxes. When they see a radio group, they often know they can only choose one and choosing one will uncheck the others. It would really make your life easier and a more pleasant experience for your users – codeMagic Apr 09 '14 at 18:01
  • can you show an example with respect to my code? i have posted my xml –  Apr 09 '14 at 18:03
  • [Here is an example of xml](http://stackoverflow.com/questions/17157705/radiogroup-allows-multiple-radiobuttons-to-be-selected/17157982#17157982) – codeMagic Apr 09 '14 at 18:04
  • [Here is a decent example in the docs](http://developer.android.com/guide/topics/ui/controls/radiobutton.html) – codeMagic Apr 09 '14 at 18:06
  • that looks neat, but I want to keep my categories as buttons, just the dynamic checkboxes need to be turned to radio buttons, any idea how to do it programmatically –  Apr 09 '14 at 18:09
  • Plus `RadioGroup` suits your profile pic better – Emmanuel Apr 09 '14 at 18:10
  • @Emmanuel how does it? haha –  Apr 09 '14 at 18:11
  • Anything that has to deal with `Radio` and Bieberish pics goes together – Emmanuel Apr 09 '14 at 18:13
  • Does something change that they have to be dynamic? Creating it in xml is much easier – codeMagic Apr 09 '14 at 18:14
  • 1
    @Emmanuel That pic is older than bieber haha, I was around 20 years old , now 27, he copied my style haha –  Apr 09 '14 at 18:14
  • @codeMagic I created them as dynamic as I am retrieving the data from database.Also, multiple checks are allowed in other view with common classes so wanted to keep it consistent, is there no other way I can recognize current click position and uncheck all other checks? –  Apr 09 '14 at 18:15
  • You can but it just seems bad. You can put your check boxes in an array and when one is checked you iterate through and uncheck the others – codeMagic Apr 09 '14 at 18:17
  • @codeMagic it should be fine, just trying to dig my ways around it, would be a good thing to learn with current position, can you explain that with code if you have a clue about it? –  Apr 09 '14 at 18:20
  • I posted an answer. See if that makes sense. I just wrote it quickly so you may need to adjust some variables but that should give you an idea to what I mean – codeMagic Apr 09 '14 at 18:34

1 Answers1

0

I'm not sure where you are initializing your CheckBoxes so I'm not exactly sure where you will put this. But when you create them, just add them to an ArrayList like

//initialize an ArrayList
ArrayList<CheckBox> checkArray = new ArrayList<CheckBox>();
// then add your checkboxes to the list
checkArray.add(aCheckBox);

then in your click event

for(i=0; i<checkArray.size(); i++)
{
    if (checkArray.get(i) != (CheckBox)v)  // assuming v is the View param passed in
       checkArray.get(i).setChecked(false);
}

This could be written better but I'm not sure how the rest of your code looks so this should give you an idea.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • This is a part of my question here : http://stackoverflow.com/questions/22964603/how-do-i-set-one-button-with-a-checkbox-active-at-a-time-in-my-code-that-retriev it has some code in detail if that makes sense –  Apr 09 '14 at 18:46
  • I don't know what you would "getcurrentpositionof"? Ok, I will try to look – codeMagic Apr 09 '14 at 18:47
  • thanks! Also, I made your modification to the base class where I am using it and added the code to that question for more info –  Apr 09 '14 at 18:53