I want to create layout for Custom Adapter. It should be line with checkbox, icon and textView.
I use next layout xml configuration for row:
<LinearLayout
android:id="@+id/list_item_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkboxFileSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:button="@drawable/checkbox_icon"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:checked="false"
android:visibility="visible"/>
<ImageView
android:id="@+id/folder_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/folder_icon"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/current_folder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="@color/primaryText"/>
</LinearLayout>
but left and top padding doesn't work. When I add left and right padding it only increase right padding value.
example image here
You have to use android:layout_marginLeft
for this usecase.
Padding is the space inside the component. e.g: space between the text and the border of TextView.
Margin is the space outside the components. e.g: space between left edge of the screen and border of your component
A bit late to the party but here's a solution for anyone seeking a way to do it. Since there's no way you can move the check box itself without adding margin, you can override the class:
class PaddedCheckBox @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = android.R.attr.checkboxStyle,
) : MaterialCheckBox(context, attrs, defStyle) {
override fun onDraw(canvas: Canvas) {
val rtlCoefficient = if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL) -1 else 1
canvas.save()
canvas.translate(rtlCoefficient * 40f, 0f)
super.onDraw(canvas)
canvas.restore()
}
}
You can extend your view from AppComptCheckBox
itself in case you're not using material components.
Note: Consider adding excessive padding end to your check box to compensate the canvas translation.
One way I found to accomplish this is to "repackage" the CheckBox
's button drawable with insets. In Kotlin:
myCheckbox.buttonDrawable = InsetDrawable(myCheckbox.buttonDrawable, leftPadding, 0, 0, 0)
...where leftPadding
is the amount of padding you want to add.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With