So, I have my buttons lined up like below:
Here's an image of what I'm trying to achieve:
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);
}