When I change a button of checkbox from xml, checkbox doesn't reflect checked.
My xml file is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/memberName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="57.56"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"/>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox" <----- change button of checkbox
/>
#drawable/checkbox is png file
In order to work android:button needs to be to be set to a selector xml file that defines drawables for both checked and unchecked states. Try setting it up like this:
checkbox_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/checkbox_checked" />
<item android:state_checked="false" android:drawable="#drawable/checkbox_unchecked" />
</selector>
where checkbox_checked.png is the image you want to use when the box is checked. and checkbox_unchecked.png is the image when it is not checked.
Then in your main layout set the button like this:
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
/>
Related
I have a Toggle Button as:
<ToggleButton
android:id="#+id/tv_pmpSwitch"
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="wrap_content"
android:background="#drawable/toggle_view"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"
android:paddingTop="16dp"
android:layout_marginLeft="16dp"
/>
And my toggle_view drawable is :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/ic_list_action"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/ic_grid_action"
android:state_checked="false"/>
</selector>
I don't understand why the image in background is stretched ?? I've tried various size images.
You can simply set in the xml layout file:
android:minWidth="0dp"
android:minHeight="0dp"
The image will not stretch anymore
<ToggleButton
android:id="#+id/tv_pmpSwitch"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_weight="0.1"
android:background="#drawable/toggle_view"
android:textOff=""
android:textOn="" />
Try this on your code and adjust only layout height parameter!
EDIT
The way to get a non stretched image is using a bitmap and not a drawable.
use following as your xml as your background.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<bitmap android:src="#drawable/ic_list_action"
android:gravity="center_vertical|left" />
</item>
<item>
<bitmap android:src="#drawable/ic_grid_action"
android:gravity="center_vertical|left" />
</item>
</layer-list>
After trying out a few things I found what was wrong:
The weightSum was the culprit and the assigned weight was stretching the image.
<LinearLayout
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="wrap_content">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_pmpSwitch"
android:background="#drawable/ic_toggle_list"
/>
</LinearLayout>
Putting the whole code inside a LL parent did the trick
Can someone tell me how can I create a radioGroup with radio buttons that will have the look like on the image:
Here is my xml code:
<RadioGroup
android:id="#+id/network_creation_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="20dp">
<RadioButton
android:id="#+id/network_creation_private"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:drawableRight="#drawable/lock"
android:text="Private"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
style="#style/TextView_LightGrey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Anyone can join" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="#color/dark_gray" />
<RadioButton
android:id="#+id/network_creation_public"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:drawableRight="#drawable/unlock"
android:text="Public"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
style="#style/TextView_LightGrey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Join via a suggest from existing\nnetwork members" />
</RadioGroup>
Radio Button Selector xml
res/drawable/radio_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_selected" android:state_checked="true"/>
<item android:drawable="#drawable/btn_unselected" android:state_checked="false"/>
</selector>
Now add android:background="#drawable/radio_selector" android:drawableRight="#drawable/radio_selector" to every radio button tag.
Instead of radio button you can use Checked Text View here and here is the example but in this you have to handle the checked states like radio group. Means if one is checked you have to make another one is unchecked.
i have tried out the suggestions given but unable to reduce the size...on reduction of size in graphical layout,the border lines of the checkbox disappear...iam new to android...any help will be appreciated
thanks
here is code...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3CAE5"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="8dp" >
<CheckBox
android:id="#+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/txt_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000" />
<TextView
android:id="#+id/txt_fName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000" />
</LinearLayout>
height and width change removes the border lines...check box appears in a list view that is populated dynamically...
the below code works for changing the checkbox image but now iam unable to check the box ie the tick mark does not appear on checking the box???
The text determines the size of the check box. try this:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
....... />
use custom image for check box
and use image of your size
<CheckBox
android:id="#+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/checked" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/unchecked" />
<item android:state_checked="false"
android:drawable="#drawable/unchecked" />
<item android:state_checked="true"
android:drawable="#drawable/checked" />
</selector>
I was using the standard Android checkbox, but when I installed my app on other peoples phones for testing, the checkbox was different on each phone.
On one of the phones, the checkbox was so light you could hardly see the box when it was checked.
So I tried to make a custom checkbox checked image.
This worked ok, but I can still see the original deafult checkbox image in the background?
Any ideas why?
Here is my checkable linear layout, to be honest I'm not even sure if I need this?
<?xml version="1.0" encoding="utf-8"?>
<com.myapp.myappname.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:background="#drawable/customcheckbox"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</com.myapp.myappname.CheckableLinearLayout>
Then I have a custom list row which is my custom ListView:
<?xml version="1.0" encoding="utf-8"?>
<com.myapp.myappname.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- Thumbnail image -->
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/list_image"
android:layout_width="60dip"
android:layout_height="60dip"
android:focusable="false"
/>
</LinearLayout>
<!-- File Name -->
<TextView
android:id="#+id/filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:textColor="#343434"
android:textSize="12dip"
android:layout_marginTop="10dip"
android:layout_toRightOf="#+id/thumbnail"
android:focusable="false"
android:text="Some Text." />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_alignParentRight="true"
android:background="#drawable/customcheckbox"
android:enabled="false" />
</RelativeLayout>
</com.myapp.myappname.CheckableLinearLayout>
Any ideas how to suppress the original?
Custom checkbox:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--<item android:state_checked="false" android:drawable="#drawable/unchecked" />-->
<item android:state_checked="true" android:drawable="#drawable/checked" />
<!--<item android:drawable="#drawable/unchecked" /> <!– default –>-->
</selector>
The attribute you want to change is button, not background.
First, design your custom graphics for different button states. Use a state list drawable (docs) to associate the various images. For example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/checked" />
<item android:state_checked="false" android:drawable="#drawable/unchecked" />
</selector>
Save this into your drawable resources folder, something like my_checkbox.xml.
Now, use the button attribute on your checkbox layout definition:
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_alignParentRight="true"
android:button="#drawable/my_checkbox"
android:enabled="false" />
I have troubles making my RelativeLayout change background drawable on click. I made it clickable and focusable but this make nothing change. The background remains static to enables drawable. Thanks in advance.
<RelativeLayout
android:id="#+id/bttn_type_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="#drawable/day_bttn_bg_red"
android:clickable="true"
android:focusable="true"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<ImageView
android:id="#+id/bttn_type_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:contentDescription="#string/app_name"
android:src="#drawable/relationship" />
<TextView
android:id="#+id/bttn_type_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bttn_type_iv"
android:layout_centerHorizontal="true"
android:layout_marginTop="1dp"
android:text="#string/type_bttn_txt"
android:textSize="#dimen/data_fill_bttn_txt_size"
android:textStyle="bold" />
</RelativeLayout>
Here is the selector xml in drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bttn_red_p_bg" android:state_enabled="false" android:state_pressed="true"></item>
<item android:drawable="#drawable/bttn_red_a_bg" android:state_enabled="true"></item>
</selector>