engrjd91 0 Junior Poster in Training

I am creating an android application .. in my app.. one of the activity contains 2 radio buttons..i have 2 questions for it...

  1. How to select only one radio button at a time, what code will be written?
  2. How to decrease the size of the radio icon (Circular icon only, not text)?

I can show u my xml file..

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android=""
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        style="@style/headings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="30dp"
        android:text="Basic Electrical Engineering"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Number of Sessions"
        android:textSize="20sp" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:text="1"
        android:textSize="16sp" />

    <RadioButton
        android:id="@+id/RadioButton01"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:text="2"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingTop="15dp"
        android:text="Session(s) Duration"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="example: 2 hours" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingTop="15dp"
        android:text="Room"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/EditText01"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="example: A" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="clip_vertical"
        android:gravity="center_vertical"
        android:text="Begin Attendance" />

</LinearLayout>
</ScrollView>

Dani AI

Generated

— two quick, reliable fixes for your layout: use a RadioGroup to enforce single selection, and replace the default radio drawable to change only the circular icon size.

Putting the buttons in a RadioGroup (XML) makes Android enforce one selection automatically. Example layout pattern:

<RadioGroup
    android:id="@+id/sessions_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/rb_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1" />

    <RadioButton
        android:id="@+id/rb_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" />
</RadioGroup>

You can check/select programmatically and listen for changes:

RadioGroup rg = findViewById(R.id.sessions_group);
rg.check(R.id.rb_one); // select in code

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
    RadioButton rb = findViewById(checkedId);
    // use rb.getText() or take action
  }
});

To shrink just the circular icon: replace the button drawable rather than scaling the whole view. Two common approaches:

  • Create a selector that points to smaller checked/unchecked drawables, then set android:button="@drawable/radio_small_selector" on the RadioButton. The selector looks like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:drawable="@drawable/radio_checked_small" />
  <item android:drawable="@drawable/radio_unchecked_small" />
</selector>

The referenced drawables can be small PNGs or vector drawables sized to the pixel dimensions you want.

  • Or set android:button="@null" and use android:drawableLeft="@drawable/radio_unchecked_small" (and a checked variant in code) so only the icon is the custom drawable; text size remains unchanged.

Caution: keep the overall touch target at or above ~48dp for usability even if the icon is smaller. If using vector drawables on older Android versions, enable AppCompat vector support. For official behavior details see the Android RadioButton docs: RadioButton guide.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.