1

So, I have my buttons lined up like below:

Here's an image of what I'm trying to achieve:

enter image description here

In the image, the first row is just a row of buttons with no button pressed. In the second row, only the first button is pressed. In the third row, I clicked on 4 and only the fourth button is pressed (not the first one anymore).

    <RadioGroup

            android:id="@+id/toggleGroup"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:background="@color/white"
            android:useDefaultMargins="true"

            android:layout_column="0"
            android:columnCount="4"
            android:rowCount="1"
            android:orientation="horizontal"
>

    <ToggleButton

                android:id="@+id/number_zero"
                android:layout_width="34sp"
                android:layout_height="40sp"
                android:textOn="@string/number_zero"
                android:textOff="@string/number_zero"
                android:onClick="onToggle"
                android:checked="true"
                android:background="@drawable/psc_number_color"
                android:layout_margin="5sp"

                />
    <ToggleButton
                android:id="@+id/number_one"
                android:layout_width="34sp"
                android:layout_height="40sp"
                android:textOn="@string/number_one"
                android:textOff="@string/number_one"
                android:onClick="onToggle"
                android:checked="true"
                android:background="@drawable/psc_number_color"
                android:layout_margin="5sp"

            />

    <ToggleButton
                android:id="@+id/number_two"
                android:layout_width="34sp"
                android:layout_height="40sp"
                android:textOn="@string/number_two"
                android:textOff="@string/number_two"
                android:background="@drawable/psc_number_color"
                android:layout_margin="5sp"
                />
</RadioGroup>

I was wondering if it would be possible to toggle between these five buttons in such a way that if I pressed one down, the button would remain pressed. If I click on another button in that group, the button I clicked on before would depress and the new button I clicked on would be pressed down.

Trying to do this inside a pageradapter:

   public void onToggle(View view) {
        ((RadioGroup)view.getParent()).check(view.getId());
        // app specific stuff ..
    }

   public Object instantiateItem(ViewGroup parent, int position) {

                one = (ToggleButton) view.findViewById(R.id.number_one);
                two = (ToggleButton) view.findViewById(R.id.number_two);
                three = (ToggleButton) view.findViewById(R.id.number_three);
                four = (ToggleButton) view.findViewById(R.id.number_four);

                final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
                        for (int j = 0; j < radioGroup.getChildCount(); j++) {
                            final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
                            view.setChecked(view.getId() == i);
                        }
                    }
                };

                one.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        one.toggle();
                    }
                });

                ((RadioGroup) view.findViewById(R.id.toggleGroup)).setOnCheckedChangeListener(ToggleListener);

}
Kala J
  • 2,040
  • 4
  • 45
  • 85

2 Answers2

1

Try with this: I have done same things with Checkbox [ custom checkbox ].

enter image description here

@Override
public void onClick(View view) {

    CheckBox cb = (CheckBox) view;


    if(cb.isChecked()){

        LinearLayout llLayout = (LinearLayout) cb.getParent();

        for(int i=0; i<((ViewGroup)llLayout).getChildCount(); ++i) {
            View nextChild = ((ViewGroup)llLayout).getChildAt(i);
            if(nextChild instanceof CheckBox && nextChild.getId()==cb.getId() ){

            }else if (nextChild instanceof CheckBox && nextChild.getId()!=cb.getId() ){

                CheckBox cb2=(CheckBox) nextChild;
                cb2.setChecked(false);
            }
        }

    } else{
        LinearLayout llLayout = (LinearLayout) cb.getParent();

       for(int i=0; i<((ViewGroup)llLayout).getChildCount(); ++i) {
            View nextChild = ((ViewGroup)llLayout).getChildAt(i);
            if(nextChild instanceof CheckBox && nextChild.getId()==cb.getId() ){
                CheckBox cb2=(CheckBox) nextChild;
                cb2.setChecked(false);
            }else if (nextChild instanceof CheckBox && nextChild.getId()!=cb.getId() ){
                CheckBox cb2=(CheckBox) nextChild;
                cb2.setChecked(false);

            }
        }
    }
}

My XML:

<LinearLayout
        android:id="@+id/llShift"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="4"
        android:gravity="right"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/cbMorning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/custom_radio_button_morning"
            android:paddingBottom="5dp"
            android:paddingRight="3dp"
            android:paddingTop="5dp" />

        <CheckBox
            android:id="@+id/cbEvning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/custom_radio_button_evening"
            android:paddingBottom="5dp"
            android:paddingRight="5dp"
            android:paddingTop="5dp" />
    </LinearLayout>

custom_radio_button_morning.xml

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@drawable/morning_chk"
        android:state_checked="true"/>
    <item
        android:drawable="@drawable/morning_unchk"
        android:state_checked="false"/>
</selector>

enter image description here

enter image description here

  • Nvm. I fixed it. I have a question. Where should I put the java code? Right now, I tied it to one button. Should this be for all buttons in my view or..? That is, what is the listener tied to? – Kala J Oct 23 '14 at 18:34
  • I have a question. So far it works if I take off my drawable, then the toggle buttons act like radio buttons when I select between them. Problem is, when I add my drawable to my button (e.g. android:background="@drawable/psc_number_color"). The buttons no longer act like they're supposed to. I'm not sure why. – Kala J Oct 24 '14 at 14:11
  • Can I see your drawable? – Kala J Oct 24 '14 at 15:34
  • I marked your answer as best answer even though I am still having drawable issue. I put up a new question if you would like to look at it: http://stackoverflow.com/questions/26554764/could-my-drawable-be-interfering-with-my-button-state – Kala J Oct 25 '14 at 03:21
  • see my git repo. Only for you. https://github.com/iamsarker/android-apps/tree/master/ToggleButtonCustom – Md. Shahadat Sarker Oct 25 '14 at 04:41
0

You should use custom toggle button - check this link here and here

Community
  • 1
  • 1
Vivek Shah
  • 380
  • 2
  • 8