1

I want to know how to check the button radio checked, and how to tell the application that there is only one radio boutton checkable?

PS : I have create two RadioButtons in XML :

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Boutton_Bluetooth"
    android:id="@+id/BouttonRADIO_Bluetooth"
    android:layout_gravity="center_horizontal"
    android:textSize="20dp"
    android:textStyle="bold" />

<RadioButton
    android:layout_width="119dp"
    android:layout_height="wrap_content"
    android:text="@string/Boutton_RS232"
    android:id="@+id/BouttonRADIO_RS232"
    android:layout_gravity="center_horizontal"
    android:textSize="20dp"
    android:textStyle="bold"/>

Thanks in advance :)

Yvan1263
  • 80
  • 2
  • 14

3 Answers3

0

Just define your xml like,

 <RadioGroup android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Boutton_Bluetooth"
    android:id="@+id/BouttonRADIO_Bluetooth"
    android:layout_gravity="center_horizontal"
    android:textSize="20dp"
    android:textStyle="bold" />

<RadioButton
    android:layout_width="119dp"
    android:layout_height="wrap_content"
    android:text="@string/Boutton_RS232"
    android:id="@+id/BouttonRADIO_RS232"
    android:layout_gravity="center_horizontal"
    android:textSize="20dp"
    android:textStyle="bold"/>

            </RadioGroup>

hope it helps you.

Mangesh Sambare
  • 594
  • 3
  • 23
0

"I want to know how to check the button radio checked"

yourRadioButton.isChecked()

"how to tell the application that there is only one radio boutton checkable?"

-put your radioButtons into radioGroup

  • Thanks for your answer. I ask you how to place " Radiobutton.isChecked() " please? My two radiobuttons will display on a alertdialog. – Yvan1263 Mar 28 '16 at 15:40
  • for examle [link](http://www.learn-android-easily.com/2013/01/adding-radio-buttons-in-dialog.html) but if u need something more then just some radiobuttons, your can create your own dialog class and setup it as your need... – Denis Kruminsh Mar 28 '16 at 15:47
0

To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Boutton_Bluetooth"
android:id="@+id/BouttonRADIO_Bluetooth"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold" />

<RadioButton
android:layout_width="119dp"
android:layout_height="wrap_content"
android:text="@string/Boutton_RS232"
android:id="@+id/BouttonRADIO_RS232"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold"/>

</RadioGroup>

Within the Activity that hosts this layout, the following method handles the click event for both radio buttons:

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.BouttonRADIO_Bluetooth:
        if (checked)
            // BouttonRADIO_Bluetooth 
        break;
    case R.id.BouttonRADIO_RS232:
        if (checked)
            // BouttonRADIO_RS232
        break;
    }
}

Reference

Aamir Ali
  • 203
  • 1
  • 8