Adding custom radio buttons in android

前端 未结 10 770
慢半拍i
慢半拍i 2020-11-22 11:43

I am trying to get radiobutton effect for regular buttons in android

I have a simple android radio button below

相关标签:
10条回答
  • 2020-11-22 12:19

    I have updated accepted answer and removed unnecessary things.

    I have created XML for following image.

    Your XML code for RadioButton will be:

    <RadioGroup
            android:id="@+id/daily_weekly_button_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
    
            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="@dimen/_80sdp"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:background="@drawable/radio_flat_selector"
                android:button="@android:color/transparent"
                android:checked="true"
                android:paddingLeft="@dimen/_16sdp"
                android:paddingTop="@dimen/_3sdp"
                android:paddingRight="@dimen/_16sdp"
                android:paddingBottom="@dimen/_3sdp"
                android:text="Daily"
                android:textColor="@color/radio_flat_text_selector" />
    
            <RadioButton
                android:id="@+id/radio1"
                android:gravity="center"
                android:layout_width="@dimen/_80sdp"
                android:layout_height="wrap_content"
                android:background="@drawable/radio_flat_selector"
                android:button="@android:color/transparent"
                android:paddingLeft="@dimen/_16sdp"
                android:paddingTop="@dimen/_3sdp"
                android:paddingRight="@dimen/_16sdp"
                android:paddingBottom="@dimen/_3sdp"
                android:text="Weekly"
                android:textColor="@color/radio_flat_text_selector" />
    
    </RadioGroup>
    

    radio_flat_selector.xml for background selector:

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

    radio_flat_selected.xml for selected button:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners
            android:radius="1dp"
            />
        <solid android:color="@color/colorAccent" />
        <stroke
            android:width="1dp"
            android:color="@color/colorAccent" />
    </shape>
    

    radio_flat_regular.xml for regular selector:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:radius="1dp" />
        <solid android:color="#fff" />
        <stroke
            android:width="1dp"
            android:color="@color/colorAccent" />
    </shape>
    

    All the above 3 file code will be in drawable/ folder.

    Now we also need Text Color Selector to change color of text accordingly.

    radio_flat_text_selector.xml for text color selector

    (Use color/ folder for this file.)

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/colorAccent" android:state_checked="false" />
        <item android:color="@color/colorWhite" android:state_checked="true" />
    </selector>
    

    Note: I refereed many answers for this type of solution but didn't found good solution so I make it.

    Hope it will be helpful to you.

    Thanks.

    0 讨论(0)
  • 2020-11-22 12:19

    Simple way try this

    1. Create a new layout in drawable folder and name it custom_radiobutton (You can rename also)

       <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
       <item android:state_checked="false" 
      android:drawable="@drawable/your_radio_off_image_name" />
       <item android:state_checked="true" 
      android:drawable="@drawable/your_radio_on_image_name" />
      </selector>
      
    2. Use this in your layout activity

      <RadioButton
       android:id="@+id/radiobutton"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:button="@drawable/custom_radiobutton"/>
      
    0 讨论(0)
  • 2020-11-22 12:20

    You must fill the "Button" attribute of the "CompoundButton" class with a XML drawable path (my_checkbox). In the XML drawable, you must have :

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

    Don't forget to replace my_checkbox by your filename of the checkbox drawable , checkbox_not_checked by your PNG drawable which is your checkbox when it's not checked and checkbox_checked with your image when it's checked.

    For the size, directly update the layout parameters.

    0 讨论(0)
  • 2020-11-22 12:29

    In order to hide the default radio button, I'd suggest to remove the button instead of making it transparent as all visual feedback is handled by the drawable background :

    android:button="@null"
    

    Also it would be better to use styles as there are several radio buttons :

    <RadioButton style="@style/RadioButtonStyle" ... />
    
    <style name="RadioButtonStyle" parent="@android:style/Widget.CompoundButton">
        <item name="android:background">@drawable/customButtonBackground</item>
        <item name="android:button">@null</item>
    </style>
    

    You'll need the Seslyn customButtonBackground drawable too.

    0 讨论(0)
  • 2020-11-22 12:30

    Best way to add custom drawable is:

    <RadioButton
        android:id="@+id/radiocar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:button="@drawable/yourbuttonbackground"
        android:checked="true"
        android:drawableRight="@mipmap/car"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="yourtexthere"/>
    

    Shadow overlay by custom drawable is removed here.

    0 讨论(0)
  • 2020-11-22 12:31

    Below code is example of custom radio button. follow below steps..

    1. Xml file.

       <FrameLayout
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="0.5">
      
           <TextView
               android:id="@+id/text_gender"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="left|center_vertical"
               android:gravity="center"
               android:text="@string/gender"
               android:textColor="#263238"
               android:textSize="15sp"
               android:textStyle="normal"
      
               />
      
           <TextView
               android:id="@+id/text_male"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center"
               android:layout_marginLeft="10dp"
               android:gravity="center"
               android:text="@string/male"
               android:textColor="#263238"
               android:textSize="15sp"
               android:textStyle="normal"/>
      
           <RadioButton
               android:id="@+id/radio_Male"
               android:layout_width="28dp"
               android:layout_height="28dp"
               android:layout_gravity="right|center_vertical"
               android:layout_marginRight="4dp"
               android:button="@drawable/custom_radio_button"
               android:checked="true"
               android:text=""
               android:onClick="onButtonClicked"
               android:textSize="15sp"
               android:textStyle="normal"
      
               />
       </FrameLayout>
      
       <FrameLayout
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="0.6">
      
           <RadioButton
               android:id="@+id/radio_Female"
               android:layout_width="28dp"
               android:layout_height="28dp"
               android:layout_gravity="right|center_vertical"
               android:layout_marginLeft="10dp"
               android:layout_marginRight="0dp"
               android:button="@drawable/custom_female_button"
               android:text=""
               android:onClick="onButtonClicked"
               android:textSize="15sp"
               android:textStyle="normal"/>
      
           <TextView
               android:id="@+id/text_female"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="left|center_vertical"
               android:gravity="center"
               android:text="@string/female"
               android:textColor="#263238"
               android:textSize="15sp"
               android:textStyle="normal"/>
      
           <RadioButton
               android:id="@+id/radio_Other"
               android:layout_width="28dp"
               android:layout_height="28dp"
               android:layout_gravity="center_horizontal|bottom"
               android:layout_marginRight="10dp"
               android:button="@drawable/custom_other_button"
               android:text=""
               android:onClick="onButtonClicked"
               android:textSize="15sp"
               android:textStyle="normal"/>
      
           <TextView
               android:id="@+id/text_other"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="right|center_vertical"
               android:layout_marginRight="34dp"
               android:gravity="center"
               android:text="@string/other"
               android:textColor="#263238"
               android:textSize="15sp"
               android:textStyle="normal"/>
       </FrameLayout>
      

    2.add the custom xml for the radio buttons

    2.1.other drawable

    custom_other_button.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/select_radio_other" />
        <item android:state_checked="false" android:drawable="@drawable/default_radio" />
    
    </selector>
    

    2.2.female drawable

    custom_female_button.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/select_radio_female" />
        <item android:state_checked="false" android:drawable="@drawable/default_radio" />
    
    </selector>
    

    2.3. male drawable

    custom_radio_button.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/select_radio_male" />
        <item android:state_checked="false" android:drawable="@drawable/default_radio" />
    </selector>
    
    1. Output: this is the output screen
    0 讨论(0)
提交回复
热议问题