Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Checkbox - Background + Button Layout - wrong Size

I think I'm not seeing an error in one of my layouts. I want to display a Checkbox and and a border around it.

I generated the images using the Android Holo Colors Generator

I tried to add a shape to the checkbox, which worked fine on a Nexus 4, but did not display the button at all on any other device, so I added a dummy layout:

Layout:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5sp"
    android:layout_marginTop="3sp"
    android:background="@drawable/shape_button" >

    <CheckBox
        android:id="@+id/check"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:button="@drawable/btn_radio_holo_dark_hm" />
</LinearLayout>

shape:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@android:color/transparent"/>

    <stroke
        android:width="1px"
        android:color="@color/gray" />

    <corners
        android:bottomLeftRadius="5sp"
        android:bottomRightRadius="5sp"
        android:topLeftRadius="5sp"
        android:topRightRadius="5sp" />

</shape>

Now I get this:

Layout on Devices

I tried setting minWidth depending on mdpi/hdpi/xhdpi but the button never looks centered on every device. fill_parent/wrap_content does not result in any change, also moving the button inside the layout.

Any suggestions on what has gone wrong here?

like image 404
Philipp E. Avatar asked Jun 21 '26 22:06

Philipp E.


1 Answers

change your outer layout to a RelativeLayout and set centerInParent=true for checkbox

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5sp"
android:layout_marginTop="3sp"
android:background="@drawable/shape_button" 
android:padding="2sp">

<CheckBox
    android:id="@+id/check"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:button="@drawable/btn_radio_holo_dark_hm" 
    android:layout_centerInParent="true/>
</RelativeLayout>
like image 187
Asanka Senavirathna Avatar answered Jun 24 '26 13:06

Asanka Senavirathna