Android ring shape for radio button
Asked Answered
J

2

6

I need to create 2 ring shapes for my radio buttons:

  1. white circle
  2. white circle with another circle inside it with a different color

I dont have much clue on how to do this. What I tried so far:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring">
            <android:solid android:color="@color/white" />

            <android:size android:height="10dp" android:width="10dp" />

            <corners android:radius="10dp" />
        </shape></item>

</selector>

<RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:button="@drawable/radio_shape_unchecked"
                        android:checked="false"
                        android:text="Persoana fizica" />

https://i.sstatic.net/mltby.png

Jolo answered 21/8, 2013 at 8:18 Comment(0)
R
22

Here is some code for you..You can do something like this. If you have any problem then I can mail you whole project..Hope this helps you and others. !!

res/drawable/red_ring.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:innerRadiusRatio="3"
   android:shape="ring"
   android:thickness="10dp"
   android:useLevel="false" >

  <solid android:color="#FF0000" />

  <size
    android:height="30dp"
    android:width="30dp" />

</shape>

res/drawable/blue_ring.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:innerRadiusRatio="3"
   android:shape="ring"
   android:thickness="5dp"
   android:useLevel="false" >

  <solid android:color="#0000FF" />

  <size
    android:height="20dp"
    android:width="20dp" />

</shape>

res/drawable/layer.xml

  <?xml version="1.0" encoding="utf-8"?>
  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item android:drawable="@drawable/red_ring"/>
      <item android:drawable="@drawable/blue_ring"/>

  </layer-list>

res/drawable/selector_radio.xml

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

res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:gravity="center" >

    <RadioButton
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:button="@drawable/selector_radio"
        android:paddingLeft="30dp"
        android:text="Radio 1" />

    <RadioButton
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:button="@drawable/selector_radio"
        android:paddingLeft="30dp"
        android:text="Radio 2" />
   </RadioGroup>

 </RelativeLayout>

Screenshot:

OutPut

Roosevelt answered 21/8, 2013 at 9:17 Comment(9)
thanks a lot you almost gave me the exact look (what I need is something like radio 2 but the colors swapped) I'll try to edit it and get the look I need.Jolo
Cool..If you find this helpfull then do not forget to accept and upvote.Roosevelt
I wont forget but I have a problem: how do I set a border or how should I say so that the selected radio look like int the photo (link in the question) right now I can only figure out how to set it a single colorJolo
@MihaiBratulescu ohh..Sorry man..I just forgot to add layer.xml now I edited look at answer. tick ACCEPT if it worked for you.Roosevelt
Ok works but I cant seem to get the blue ring to be smaller (it is just a bit smaller) than the white one, but I cant make it any smallerJolo
Just play with attributes of shape file and I am sure you will get result...Anyway I suggest you to use two images instead of this approach (as it is quit complex).Roosevelt
Never mind I used innerRatio and it looks good. size and thickness tweaking wasn't enoughJolo
Serious problem: although it look perfect in the preview in the emulator it is just a ring with white border and no longer fully white.Jolo
@Ketan Ahir Will it work with lollipop(5.0), it just comes with a FILL color.Dellora
D
0

The Ketan Ahir solution, not work for me beacouse i need filled background. Finally a made 1 ring shape, and oval shape, and set size when display.

ring shape

 <shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring" android:useLevel="false">
    <solid
        android:color="#fbad38" />
    <stroke
        android:width="2dp"
        android:color="#fbad38" />
   </shape>

Oval Shape

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval" android:useLevel="false">
  <solid android:color="#fbad38"></solid>
</shape>

Resizing Code

    frbtRadio1 = (RadioButton) view.findViewById(R.id.radio1);

    frbtRadio1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            ViewGroup.LayoutParams lp = frbtRadio1.getLayoutParams();
            lp.height = frbtRadio1.getWidth();

            frbtRadio1.setLayoutParams(lp)
            frbtRadio1.getViewTreeObserver().removeOnGlobalLayoutListener( this );
        }
    });
Doorway answered 15/7, 2015 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.