Issue
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
What I want
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}"
Solution
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
Answered By - mhdwajeeh.95
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)