Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ring shape for radio button

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" />

http://i.stack.imgur.com/mltby.png

like image 245
Mihai Bratulescu Avatar asked Aug 21 '13 08:08

Mihai Bratulescu


People also ask

How do I change the color of a radio button circle?

To change the color of a checked button you can add or replace a state with android. R. attr. state_checked and add the color.

How do you set a button shape?

We can set custom shapes on our button using the xml tag <shape> . These xml files are created in the drawable folder too. shape can be used inside selectors . The shape can be set to rectangle (default), oval , ring , line .

What are the methods of radio button in Android?

RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user.

How do I create a horizontal radio button?

To make a horizontal radio button set, add the data-type="horizontal" to the fieldset . The framework will float the labels so they sit side-by-side on a line, hide the radio button icons and only round the left and right edges of the group.


1 Answers

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

like image 71
Ketan Ahir Avatar answered Oct 05 '22 10:10

Ketan Ahir