How to resize image of checkbox in android? - 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 .

Related

CheckBox doesn't display selector on black background?

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"
/>

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"

how do i add image in button? image with text

hi i want to create image button like this way http://imgur.com/sshdf in this url how do i create button like this? half image half text? please help me i wanna create exactly same button
<Button android:id="#+id/MyAccountMiniStatement"
style="?android:attr/buttonStyleSmall"
android:layout_height="25dip"
android:layout_width="fill_parent"
android:layout_marginRight="10dip"
android:layout_marginLeft="10dip"
android:background="#drawable/curvedplanebutton"
android:textColor="#drawable/button_text_color"
android:layout_marginTop="10dip"
android:text="Mini Statement"/>
Add this attribute to the button android:drawableLeft="#drawable/ic_launcher"
Something like this,
<Button android:id="#+id/MyAccountMiniStatement"
style="?android:attr/buttonStyleSmall"
android:layout_height="25dip"
android:layout_width="fill_parent"
android:layout_marginRight="10dip"
android:layout_marginLeft="10dip"
android:drawableLeft="#drawable/ic_launcher"
android:background="#drawable/curvedplanebutton"
android:textColor="#drawable/button_text_color"
android:layout_marginTop="10dip"
android:text="Mini Statement"/>
By providing the drawable Left, the icon will be placed to the left side of the text and also you can use the background attribute to give the white background to your whole button.
Why don't you Use ImageBUtton, I think that will be good for you
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/album_icon"
android:background="#drawable/round_button" />
Solution is to use CompoundDrawable.
Some attributes for Button and TextView:
1. android:drawableLeft
2. android:drawableTop
3. android:drawableRight
4. android:drawableBottom
And yes use android:drawablePadding to give padding between your text and image.
Use Textview and for text and android:drawableTop="#drawable/ic_launcher" for image in textview.
So after using this you can see that your textview is containing image also.
Try below code instead of your button code.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_launcher"
android:gravity="center"
android:text="Hello" />
Also you can add drawable to any side.
Top, bottom,right,left.
android:drawableBottom="#drawable/ic_launcher"
android:drawableTop="#drawable/ic_launcher"
android:drawableRight="#drawable/ic_launcher"
android:drawableLeft="#drawable/ic_launcher"

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"

Half cut text in Spinner Android

i have one layout on Android, but if i change size of spinner my text are cut, i try custom background to spinner and try layout default but not resolve.
My XML:
<Spinner
android:id="#+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="40px"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:background="#drawable/spinner_custom"
android:gravity="left|center_vertical"
android:paddingLeft="4dip"
android:textSize="16px" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="40px"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:background="#drawable/spinner_custom"
android:gravity="left|center_vertical"
android:paddingLeft="4dip"
android:textSize="16px" />
</TableRow>
how i can fix it?
Screen with custom background:
http://img814.imageshack.us/img814/8284/spinner.jpg
Is the custom background for your Spinner an image or an xml file? If it's an xml drawable file check that you aren't using padding at the bottom.
Also try changing layout_height value to wrap_content rather than setting to 40px, that may by causing the problem.

Categories

Resources