2

I want to set a margin / padding between my radiobutton drawable and the left side of it, e.g: Setting a margin of 8dp between my radiobutton drawable and the left side of the screen. I know how to set a margin between the radiobutton itself, but not how to do it with the radiobutton drawable. I also know how to set a margin right side of the radiobutton drawable with paddngStart="YOUR_PADDING".

Is this possible?

Here a picture of what I mean:

Currently

enter image description here

What I want

enter image description here

EDIT

The aboven written answer does work. For those wo want to set the value inside the layout and not programmatically, I have written a binding adapter:

@BindingAdapter("setDrawableLeftPadding")
fun setDrawableLeftPadding(view: CustomRadioButton, padding: Float) {
    view.setStartPaddingDp(padding)
}

You can then use it inside your CustomRadioButton layout with app:setDrawableLeftPadding="@{8f}"

Andrew
  • 4,264
  • 1
  • 21
  • 65
  • Try this https://stackoverflow.com/questions/28301636/android-set-padding-to-radiobutton-pin – Ali Amini Jan 10 '21 at 15:16
  • @AliAmini I already did, the first answer is useless, and I can't apply the second one because I am getting the drawable via a url and therefore can't set anything inside the `inset` – Andrew Jan 10 '21 at 15:17
  • Have you tried setting the drawable padding? See https://stackoverflow.com/a/6671544/6287910 – Cheticamp Jan 10 '21 at 16:07
  • @Cheticamp Yes I did, writing `android:paddingLeft="20dp" increases the padding between my radiobutton drawable and the text. But I want to increase the padding left side of my radiobutton drawable which does not work.. – Andrew Jan 10 '21 at 16:20

2 Answers2

2

I you want to achieve the exact same on what your second picture shows, you can write a custom RadioButton that handle this padding, the custom view code can be like this (in Kotlin):

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatRadioButton

class CustomRadioButton : AppCompatRadioButton {

    // the value of your padding (in pixels) from the start of the radio button
    var startPadding: Int = 0
        get
        set(value) {
            field = value
            requestLayout()
        }

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )


    fun setStartPaddingDp(paddingDp: Float) {
        startPaddingPx = (paddingDp * context.resources.displayMetrics.density).toInt()
    }


    override fun onDraw(canvas: Canvas?) {
        // todo: handle Right-To-Left layouts
        canvas?.translate(startPadding.toFloat(), 0f)

        super.onDraw(canvas)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        setMeasuredDimension(measuredWidth + startPadding, measuredHeight)
    }
}

You can set the padding value by setting the value of the field startPadding, for example:

yourCustomRadioButton.startPadding = 100 // this value is in pixels

// or

yourCustomRadioButton.setStartPaddingDp(100) // this value is in DP
mhdwajeeh.95
  • 417
  • 5
  • 13
  • Is it also possible to set the value in dp? Because then this solution would work out for me. And the above code changes the startpadding of my `radiobutton.drawable` and not of the entire radiobutton right? – Andrew Jan 10 '21 at 16:21
  • Sure you can transform your dp value to px, and set the padding. here is how you can do it: https://stackoverflow.com/a/62627706/3375081 – mhdwajeeh.95 Jan 10 '21 at 16:25
  • Please try the code I posted, so you can make sure if it gives you exactly what your looking for. – mhdwajeeh.95 Jan 10 '21 at 16:29
  • 1
    Yes, it does exactly what I want, thank you. But changing the px with dp would help me really much. I would accept it then as the answer – Andrew Jan 10 '21 at 16:30
  • I edited my answer, you can use the function `setStartPaddingDp` from your customRadioButton. – mhdwajeeh.95 Jan 10 '21 at 16:37
  • 1
    Thank you very much, it worked. But you have a typo in there, I think you mean `startPadding = ...` and not `startPaddingPx = ...` – Andrew Jan 10 '21 at 16:42
0
val compoundButtonDrawable = CompoundButtonCompat.getButtonDrawable(radioButton)
val insetDrawable = InsetDrawable(compoundButtonDrawable, 32, 0, 0, 0)
radioButton.buttonDrawable = insetDrawable
patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 24 '22 at 00:04