Reputation: 2070
Right now, I have a radio group as the parent and toggle buttons as the children.
So something like this:
Layout.xml
<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="11"
android:rowCount="1"
android:orientation="horizontal"
>
<ToggleButton
android:id="@+id/number_zero"
android:layout_width="34sp"
android:layout_height="40sp"
android:layout_row="0"
android:layout_column="0"
android:textOn="@string/number_zero"
android:textOff="@string/number_zero"
android:layout_margin="5sp"
/>
<ToggleButton
android:id="@+id/number_one"
android:layout_width="34sp"
android:layout_height="40sp"
android:layout_row="0"
android:layout_column="1"
android:textOn="@string/number_one"
android:textOff="@string/number_one"
android:layout_margin="5sp"
/>
</RadioGroup>
</GridLayout>
Code snippet in Adapter:
ToggleButton one;
one = (ToggleButton) view.findViewById(R.id.number_one);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttonValue = 1;
ToggleButton tb = (ToggleButton) view;
if(tb.isChecked()){
RadioGroup radioGroup = (RadioGroup) tb.getParent();
for(int i=0; i<(radioGroup).getChildCount(); ++i) {
View nextChild = (radioGroup).getChildAt(i);
if(nextChild instanceof ToggleButton && nextChild.getId()==tb.getId() ){
}else if (nextChild instanceof ToggleButton && nextChild.getId()!=tb.getId() ){
ToggleButton tb2=(ToggleButton) nextChild;
tb2.setChecked(false);
}
}
} else{
RadioGroup radioGroup = (RadioGroup) tb.getParent();
for(int i=0; i<(radioGroup).getChildCount(); ++i) {
View nextChild = (radioGroup).getChildAt(i);
if(nextChild instanceof ToggleButton && nextChild.getId()==tb.getId() ){
ToggleButton tb2=(ToggleButton) nextChild;
tb2.setChecked(false);
}else if (nextChild instanceof ToggleButton && nextChild.getId()!=tb.getId() ){
ToggleButton tb2=(ToggleButton) nextChild;
tb2.setChecked(false);
}
}
}
}
});
Above is repeated for each button I have. In this example for button zero and one.
Drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_checked="true" >
<shape>
<solid
android:color="@color/button_selected" />
<stroke
android:width="1dp"
android:color="@color/button_selected" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item >
<item>
<shape android:state_checked="false">
<solid
android:color="@color/number_button_grey" />
<stroke
android:width="1dp"
android:color="@color/number_button_grey" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Behavior I want:
I want switch between buttons so that only one button is pressed down at a time. E.g in this case, if I click on button zero, zero will be pressed and button one is not pressed. If I click on button one, button one is pressed and button zero is not. I want the pressed state to be true.
For some reason, if I remove my drawable for my button above, I can easily toggle like radiobutton behavior and when I press on a button, it remains on the pressed state. However, when I apply my drawable, the button does not remain pressed when down. The state changes only if I click on the button and hold the button with my finger. If I remove my finger, it comes back to state pressed = false.
I'm not sure why with the drawable, it's not the behavior I want.
EDIT: Here's a video on the behavior when I change my toggle buttoms to radio buttons and also when I have my toggle buttons. I switch beween radio buttons and the red selection does not stay put. I do the same with the toggle buttons. https://www.dropbox.com/s/9sinptgoucv1hvn/VIDEO0043.mp4?dl=0
Upvotes: 1
Views: 74
Reputation: 849
I think you are looking for this:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="#696969"
android:baselineAligned="false"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/toggleGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_column="0"
android:background="#FFFFFF"
android:columnCount="4"
android:orientation="horizontal"
android:rowCount="1"
android:useDefaultMargins="true" >
<ToggleButton
android:id="@+id/number_one"
android:layout_width="34sp"
android:layout_height="40sp"
android:layout_margin="5sp"
android:background="@drawable/button_style"
android:textOff="1"
android:textOn="1" />
<ToggleButton
android:id="@+id/number_two"
android:layout_width="34sp"
android:layout_height="40sp"
android:layout_margin="5sp"
android:background="@drawable/button_style"
android:textOff="2"
android:textOn="2" />
<ToggleButton
android:id="@+id/number_three"
android:layout_width="34sp"
android:layout_height="40sp"
android:layout_margin="5sp"
android:background="@drawable/button_style"
android:textOff="3"
android:textOn="3" />
<ToggleButton
android:id="@+id/number_four"
android:layout_width="34sp"
android:layout_height="40sp"
android:layout_margin="5sp"
android:background="@drawable/button_style"
android:textOff="4"
android:textOn="4" />
<ToggleButton
android:id="@+id/number_five"
android:layout_width="34sp"
android:layout_height="40sp"
android:layout_margin="5sp"
android:background="@drawable/button_style"
android:textOff="5"
android:textOn="5" />
</RadioGroup>
</LinearLayout>
button_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:endColor="#99BCA259"
android:startColor="#99BCA259"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#99515A91" />
<corners
android:radius="0dp" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#99BCA259"
android:startColor="#99BCA259"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#99BCA259" />
<corners
android:radius="0dp" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
</item>
<item android:state_checked="true" >
<shape>
<gradient
android:endColor="#99BCA259"
android:startColor="#99BCA259"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#99BCA259" />
<corners
android:radius="0dp" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
</item>
<item android:state_enabled="false">
<shape>
<gradient
android:endColor="#99454545"
android:startColor="#99454545"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#99454545" />
<corners
android:radius="0dp" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#99454545"
android:startColor="#99454545"
android:angle="270" />
<stroke
android:width="0dp"
android:color="#99454545" />
<corners
android:radius="0dp" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
</item>
</selector>
MainAcitivity.java
public class MainActivity extends Activity implements OnClickListener{
ToggleButton tb1, tb2, tb3, tb4, tb5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb1 = (ToggleButton) findViewById(R.id.number_one);
tb2 = (ToggleButton) findViewById(R.id.number_two);
tb3 = (ToggleButton) findViewById(R.id.number_three);
tb4 = (ToggleButton) findViewById(R.id.number_four);
tb5 = (ToggleButton) findViewById(R.id.number_five);
tb1.setOnClickListener(this);
tb2.setOnClickListener(this);
tb3.setOnClickListener(this);
tb4.setOnClickListener(this);
tb5.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
ToggleButton tb = (ToggleButton) v;
RadioGroup llLayout = (RadioGroup) tb.getParent();
for(int i=0; i<((ViewGroup)llLayout).getChildCount(); ++i) {
View nextChild = ((ViewGroup)llLayout).getChildAt(i);
ToggleButton cb2=(ToggleButton) nextChild;
if(nextChild instanceof ToggleButton && nextChild.getId()==tb.getId() ){
//cb2.setChecked(false);
}else if (nextChild instanceof ToggleButton && nextChild.getId()!=tb.getId() ){
cb2.setChecked(false);
}
}
}
}
You can download my git repo.
Upvotes: 1