CheckBox doesn't display selector on black background? - android

I'm trying add a CheckBox, the problem is the background of LinearLayout is black and the selector of CheckBox doesn't display to check. Checkboxes are Brinde and Entrega Futura in picture.
How could I solve it ?
<CheckBox
android:id="#+id/cbEntregaFutura"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Entrega Futura"
android:textColor="#FFF"
android:layout_weight="1"
/>

Related

How to resize image of checkbox in android?

I am adding image for a CheckBox widget (that is instead of the default one for both clicked and unclicked ones in .xml file using selector) it's getting added, but the image can't be resized into a smaller one and remains in its original size.
How could I decrease the image size?
The code I used to create the CheckBox is
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:button="#drawable/check_box"
android:drawablePadding="20dp"
android:text="check box text"
android:textColor="#color/labelColor" />
Set the android:button field to null and to set the checkbox as the android:background field of the checkbox
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:drawablePadding="20dp"
android:text="check box text"
android:background="#drawable/check_box" />
To set the android:button field to null and to set the checkbox states as the android:background field of the checkbox.
Your code would then become:
<CheckBox
android:id="#+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:background="#drawable/check_box"
android:drawablePadding="20dp"
android:text="check box text"
android:textColor="#color/labelColor" />
#berry: Please set android:button="#null" and set this in your android:background .
android:button="#null"
android:background="#drawable/check_box"
CheckBox doesn't respect the size of the android:button drawable when the layout width and height are set to wrap_content. So use android:background .

Checkbox with custom background and no border doesn't check

I have a custom checkbox with no border and custom background:
<CheckBox
android:id="#+id/ricordami"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center_vertical"
android:background="#color/gray"
android:button="#android:color/transparent" />
but when i launch my app, checkbox on click doesn't show check flag. How can i fix it?

How to remove internal Image padding (Top, Left and Bottom) of an Android Checkbox

I have a Checkbox in Android with a TextView above it:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/default_margin">
<TextView
android:id="#+id/tv_edit_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:layout_margin="0dp"
android:gravity="center_vertical|center_horizontal"
android:text="#string/edit_checkbox" />
<CheckBox
android:id="#+id/cb_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:checked="true" />
</LinearLayout>
The problem with Checkbox however is that it's internal "Image" got an invisible border around it. How to remove this? I've tried android:padding="-5dp" but this only worked for the right side of the CheckBox for some reason, not the top, left and bottom (my main concern is the Top).
Here is a screenshot of the Checkbox in Eclipse Graphical Layout window:
LineairLayout selected:
Checkbox selected:
In the second picture you can see the Checkbox has some padding around it when you select it. I want to have this removed (especially on the top), so that the space between the Text and the Checkbox is reduced.
Here is a picture when I have padding="-5dp":
Checkbox selected:
PS: If someone knows a solution with just a Checkbox and it's Text-field, so I can removed the LineairLayout and TextView, while still having the Text above the Checkbox I would also appreciate it. A single Checkbox with a custom style and Text is better for performance than a Checkbox inside a LineairLayout with an additional TextView. (I know this is more something for a different question, but I just add it here since it doesn't have a lot of priority. Everything is working already, except for the padding of the Checkbox.)
Ok, I changed the LineairLayout to a RelativeLayout, and than I can change the margin to a minus dp. This is my end result:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/default_margin">
<TextView
android:id="#+id/tv_edit_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:singleLine="false"
android:gravity="center_horizontal"
android:text="#string/edit_checkbox" />
<CheckBox
android:id="#+id/cb_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_edit_checkbox"
android:layout_centerHorizontal="true"
android:layout_marginTop="-10dp"
android:checked="true" />
</RelativeLayout>
This gives the following result:
RelativeLayout selected:
This does change the space between the Text and the Checkbox. For me this is what I want, however it still doesn't explain why the Android Checkbox' internal Image got a padding at its Top, Left and Bottom sides.. Now idea who of Android had this idea, but it was a pretty bad one.. It was better to just use no padding on the internal image and then have some default internal Margin/Padding that can be changed by users..
OR
Edit :
You can also get the same result with LinearLayout :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="0dp"
android:text="For all products"
android:textSize="10sp" />
<CheckBox
android:id="#+id/checkBox_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:padding="0dp" />
</LinearLayout>

spacing for checkmark image of the check box in android

Hi i am trying to create a check box list which should looks like in the below image which will have space before and after checkmark image.
but when i am trying to create check box its is showing like the below image.
there is no space surrounds to check-mark image like i am showing the first image. If i use padding to check box component its like "android:padding=50dp" its giving space between check-mark image and text. But not giving space in the starting .i.e before Check-Mark image.
below is my xml layout for check box
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dp">
<CheckBox android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:button="#drawable/btn_check_square"
android:background="#drawable/filter_item_background"
android:textColor="#android:color/black"
android:padding="5dp"
android:text="check box text"/>
</LinearLayout>
Any help to solve this issue is appreciated.
this is resolved with below code.
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/filter_item_background"
android:button="#android:color/transparent" //Make button null
android:drawableLeft="#drawable/btn_check_square" //Create button in drawable
android:drawablePadding="20dp" //Set space between Text & CB
android:padding="5dp"
android:text="check box text"
android:textColor="#android:color/black" />
Try this (without custom graphics):
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#android:color/transparent"
android:drawablePadding="8dp"
android:text="CheckBox" />
It uses a little trick: As Checkbox is derived from Button, we can add a drawable and can now set a padding between label and drawable. As we do not acutally need a real icon, we use the transparent color instead.
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/filter_item_background"
android:button="#null" //Make button null
android:drawableLeft="#drawable/btn_check_square" //Create button in drawable
android:drawablePadding="20dp" //Set space between Text & CB
android:padding="5dp"
android:text="check box text"
android:textColor="#android:color/black" />
Use the below code
final float scale = this.getResources().getDisplayMetrics().density;
checkbox.setPadding(checkbox.getPaddingLeft()
+ (int) (10.0f * scale + 0.5f),
checkbox.getPaddingTop(),
checkbox.getPaddingRight(),
checkbox.getPaddingBottom());
Add this to your checkbox:
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"

ImageButton and TextView different states

I have a set of Views consisting in an ImageButton, an ImageView and a TextView.
The code looks like this:
<ImageButton
android:id="#+id/imagebutton_com"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/bean_bg"
/>
<ImageView
android:id="#+id/imageview_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/arrow_1"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="#+id/textview_wizard_main_button"
android:text="Soybeans"
android:layout_toRightOf="#id/imageview_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_margin="10dp"
/>
All works as expected, but I want to change the textColor of the TextView when the Button is focused.
I tried writing a selector xml file to do the same, but nothing worked.
Any suggestion?
You need to do it in code. Use OnFocusChangeListener on your ImageButton to achieve this.
Create a folder color under resources and add selector.xml file to that folder
change property of TextView namely
android:textColor = "#color/white"
to
android:textColor = "#color/selector"

Categories

Resources