Adrian L
Adrian L

Reputation: 473

How to make titled border for RadioGroup in Android?

As in the title - is it possible to make a titled border around RadioGroup? Or at least a plain border ... Thanks

Upvotes: 6

Views: 4724

Answers (1)

marhab
marhab

Reputation: 29

Titled border, I don't know...You might want to just add a TextView widget above the border. But for a plain border, here's how I put a border around my radio group: Here is my basic radio group (has three buttons, horizontal)

<RadioGroup android:id="@+id/myRadioLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="38dp"
    android:orientation="horizontal"
    android:elevation="24dp">

    <RadioButton android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioBtn1"
        android:text="RadioButton1" android:checked="true"
        style="@android:style/Widget.CompoundButton.RadioButton"
        android:textSize="14sp">
    </RadioButton>



    <RadioButton android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioBtn2"
        android:text="RadioButton2"
        style="@android:style/Widget.CompoundButton.RadioButton"
        android:textSize="14sp">
    </RadioButton>

    <RadioButton android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioBtn3"
        android:text="RadioButton3" android:checked="true"
        style="@android:style/Widget.CompoundButton.RadioButton"
        android:textSize="14sp">
    </RadioButton>  
</RadioGroup>

So, if I want a border surrounding my radio button group, I add an xml file to my drawable folder. Let's call it "border.xml". Here is the code for border.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="5px" android:color="#000000" />
</shape>

Then, you add the following code to your radio group xml:

<RadioGroup android:id="@+id/myRadioLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="38dp"
    android:orientation="horizontal"
    android:background="@drawable/border"
    android:elevation="24dp">

And a border should appear around your radio group. Note that you can vary the attributes to get different shapes for the border, gradients, rounded corners, etc. possible attributes for the shape element, can be found here:

https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

Upvotes: 2

Related Questions