What is the best way to make a group of buttons that you can select and activate yourself?

I am trying to make a group of buttons in android that you can select and activate only one of them. I need to work with the same logic of a radio group and radio objects.

I tried many alternatives, but I need the most efficient way. How can i do this?

+7
android android-button
source share
3 answers

enter image description here

You can use this simple way:

1.activity_button_group.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A" android:id="@+id/btn0" android:layout_gravity="center_vertical" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B" android:id="@+id/btn1" android:layout_gravity="center_vertical" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C" android:id="@+id/btn2" android:layout_gravity="center_vertical" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="D" android:id="@+id/btn3" android:layout_gravity="center_vertical" /> </LinearLayout> 

2.ButtonGroupActivity.java

 public class ButtonGroupActivity extends Activity implements View.OnClickListener{ private Button[] btn = new Button[4]; private Button btn_unfocus; private int[] btn_id = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button_group); for(int i = 0; i < btn.length; i++){ btn[i] = (Button) findViewById(btn_id[i]); btn[i].setBackgroundColor(Color.rgb(207, 207, 207)); btn[i].setOnClickListener(this); } btn_unfocus = btn[0]; } @Override public void onClick(View v) { //setForcus(btn_unfocus, (Button) findViewById(v.getId())); //Or use switch switch (v.getId()){ case R.id.btn0 : setFocus(btn_unfocus, btn[0]); break; case R.id.btn1 : setFocus(btn_unfocus, btn[1]); break; case R.id.btn2 : setFocus(btn_unfocus, btn[2]); break; case R.id.btn3 : setFocus(btn_unfocus, btn[3]); break; } } private void setFocus(Button btn_unfocus, Button btn_focus){ btn_unfocus.setTextColor(Color.rgb(49, 50, 51)); btn_unfocus.setBackgroundColor(Color.rgb(207, 207, 207)); btn_focus.setTextColor(Color.rgb(255, 255, 255)); btn_focus.setBackgroundColor(Color.rgb(3, 106, 150)); this.btn_unfocus = btn_focus; } } 
+9
source share

While you can set this in code, as Denny Schuldt suggested, a “cleaner” way is to do it in xml (for example, drawable/radio.xml ):

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_checked" android:state_checked="true" /> <item android:drawable="@android:color/transparent" /> </selector> 

And set it as the button background: android:background="@drawable/radio"

+5
source share

You can still use the radio buttons within the group of radio stations and attributes so that each switch looks like a button.

In the xml for each switch, set android:button="@null" so that the dots are not visible. You can add a few add-ons to make them look different.

In the code, install the CheckedChangeListener in your radio group, then find the link to checkedView (RadioButton) with checkedId. In this example, I just changed the background color in the view, but you could add another background too. If the radioButton is not null, then it has already been changed, so I will reset it to its initial state.

 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (radioButton != null) { radioButton.setBackgroundColor(Color.TRANSPARENT); radioButton.setButtonDrawable(0); // removes the image } radioButton = (RadioButton) group.findViewById(checkedId); radioButton.setBackgroundColor(Color.YELLOW); radioButton.setButtonDrawable(R.drawable.icon); //sets the image } }); 

Hope this helps!

+3
source share

All Articles