안드로이드에서 라디오 그룹(RadioGroup)이란?
본격적인 예제에 들어가기 전에, 안드로이드에서 라디오 그룹(RadioGroup)이 무엇인지 먼저 알아보겠습니다.
라디오 그룹은 여러 개의 라디오 버튼(RadioButton)을 하나의 그룹으로 묶어 관리하는 컨테이너 위젯입니다. 라디오 버튼을 사용하면 사용자가 제시된 선택지 중 원하는 항목을 선택할 수 있으며, 같은 그룹 내에서는 단 하나의 항목만 선택할 수 있다는 점이 체크박스와 가장 큰 차이입니다. 즉, 하나를 선택하면 이전에 선택했던 항목은 자동으로 해제됩니다.
이번 글에서는 안드로이드 앱에서 라디오 그룹을 실제로 활용하는 방법을 단계별 예제와 함께 살펴보겠습니다.
Step 1 — 새 프로젝트 생성
Android Studio를 실행한 뒤, 상단 메뉴에서 File → New Project를 선택하고 프로젝트 생성에 필요한 정보를 모두 입력하여 새 프로젝트를 만듭니다.
Step 2 — 레이아웃 XML 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your favourite subject"
android:textSize="20sp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<RadioButton
android:id="@+id/adroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"/>
<RadioButton
android:id="@+id/java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="java"/>
<RadioButton
android:id="@+id/cpp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CPP"/>
<RadioButton
android:id="@+id/clan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C Programming"/>
</RadioGroup>
<Button
android:id="@+id/buton"
android:text="ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>위 코드는 총 네 개의 라디오 버튼(Android, Java, CPP, C Programming)을 포함하는 라디오 그룹과, 결과를 확인하기 위한 'ok' 버튼으로 구성되어 있습니다. 사용자가 'ok' 버튼을 클릭하면 현재 선택된 라디오 버튼의 텍스트 값을 가져오게 됩니다.
Step 3 — MainActivity.java 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioButton radioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button=findViewById(R.id.buton);
final RadioGroup radioGroup=findViewById(R.id.radioGroup);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int clickedRadioButton=radioGroup.getCheckedRadioButtonId();
radioButton=findViewById(clickedRadioButton);
if(clickedRadioButton ==-1){
button.setError("Error");
} else {
button.setError(null);
Toast.makeText(MainActivity.this,radioButton.getText(),Toast.LENGTH_LONG).show();
}
}
});
}
}코드 동작 방식 살펴보기
핵심 로직은 다음과 같습니다.
1. 선택된 라디오 버튼 ID 가져오기 — radioGroup.getCheckedRadioButtonId() 메서드를 호출하면 현재 선택된 라디오 버튼의 ID를 반환받을 수 있습니다.
2. 선택 여부 검증 — 반환된 값이 -1이라면 아무것도 선택하지 않은 상태이므로, 버튼에 에러 표시를 설정해 사용자에게 알려줍니다.
3. 결과 출력 — 정상적으로 선택된 항목이 있다면 해당 라디오 버튼 객체를 찾아 그 텍스트를 토스트(Toast) 메시지로 화면에 보여줍니다.
Step 4 — 앱 실행하기
manifest.xml 파일은 별도로 수정할 필요가 없습니다. 이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기를 컴퓨터에 연결했다고 가정하고, Android Studio에서 프로젝트의 액티비티 파일 중 하나를 열고 툴바의 Run(실행) 아이콘을 클릭하세요. 목록에서 자신의 모바일 기기를 선택하면 앱이 설치되어 실행됩니다.
앱이 실행되면 아래와 같은 기본 화면이 표시됩니다.

원하는 과목을 선택해 보겠습니다. 여기서는 예시로 java를 선택했습니다.

이제 'ok' 버튼을 클릭하면 라디오 그룹에서 선택된 라디오 버튼의 텍스트를 확인할 수 있습니다.

실행 결과
'ok' 버튼을 클릭하면 위 결과 화면처럼 선택된 라디오 버튼의 텍스트가 토스트 메시지로 표시됩니다. 만약 아무 항목도 선택하지 않고 버튼을 누르면 에러 메시지가 나타나므로, 선택 검증 로직도 함께 확인할 수 있습니다.
이처럼 라디오 그룹을 활용하면 성별, 결제 수단, 설정 옵션처럼 여러 선택지 중 단 하나만 골라야 하는 상황에서 직관적이고 안전한 UI를 손쉽게 구현할 수 있습니다.