How to Change Default Images of Checkbox

How to change default images of CheckBox

Drawable customdrawablecheckbox.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/unchecked_drawable" />
<item android:state_checked="true" android:drawable="@drawable/checked_drawable" />
<item android:drawable="@drawable/unchecked_drawable" /> <!-- default state -->
</selector>

yourcheckbox xml:

<CheckBox
android:id="@+id/chk"
android:button="@drawable/customdrawablecheckbox"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

How to remove default images of checkbox

You need to set android:button property :

<CheckBox android:button="@drawable/your_drawable" />

your_drawable.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_1" android:state_checked="false"/>
<item android:drawable="@drawable/ic_2" android:state_checked="true"/>
</selector>

How to set the default background for android checkbox

Create a new selector file in drawable folder with name checkbox_selection :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/checked"
android:state_checked="false"/>
<item android:drawable="@drawable/uncheck"
android:state_checked="true"/>
<item android:drawable="@drawable/checkbox"/>
</selector>

Modify your checkbox view in xml file as show below :

<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_selection"
android:text="CheckBox"
android:textColor="@color/Black" >
</CheckBox>

How can replace default checkbox style with png image in xaml wpf

In this particular case, you will need to fiddle with the Template of the CheckBox.

<CheckBox IsChecked="{Binding AirTemperatureGridChecked}">
<CheckBox.Template>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid>
<Image x:Name="Foo" Width="16" Height="16" Source="https://cdn4.iconfinder.com/data/icons/cc_mono_icon_set/blacks/48x48/checkbox_unchecked.png" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="Foo" Property="Opacity" Value="0.5" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>

I've made this one from scratch to just give an example of how quick and easy it is to get your own templates up and running for the design of controls.

As you can see, it's made of a grid, with your image inside, and below that is a trigger that will make the checkbox lower its opacity when unchecked.

You can put just about anything in that grid to design your checkbox as you would any window, and give it functionality with triggers.

Additionally (as giving each and every checkbox this mass of code would be unreasonable) you can give the controltemplate a key:

<ControlTemplate x:Key="WhateverYouWantToCallMe" TargetType="{x:Type CheckBox}">
<!-- Content ect... -->
</ControlTemplate>

Put it inside a Resource Dictionary and call it as a StaticResource for the checkbox instead, like so:

<CheckBox Template="{StaticResource WhateverYouWantToCallMe}"/>

Change checkbox check image to custom image

Try:

<input type="checkbox" class="input_class_checkbox">

jQuery

$('.input_class_checkbox').each(function(){
$(this).hide().after('<div class="class_checkbox" />');

});

$('.class_checkbox').on('click',function(){
$(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'))
});

Fiddle:
http://jsfiddle.net/cn6kn/

$('.input_class_checkbox').each(function(){

$(this).hide().after('<div class="class_checkbox" />');

});

$('.class_checkbox').on('click',function(){

$(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'))

});
.class_checkbox {

width: 20px;

height: 20px;

background-color: red;

}

.class_checkbox.checked {

background-color: green;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="checkbox" class="input_class_checkbox">

css - replacing checkbox image

It picks up the checked image when the checkbox is checked. Issue you have is the fact nothing is clickable to make the checkbox checked.

Move the AM/PM into the label and use padding to shove it over instead of the spaces.

HTML:

<div class="amPmCheckbox">
<input type="checkbox" name="data[Child][remember_me]" class="checkboxLabel main_street_input" id="am_2" value="1" />
<label for="am_2">AM</label>
</div>

CSS:

.amPmCheckbox input[type="checkbox"] {
display: none;
}
.amPmCheckbox input[type="checkbox"]+label {
background: url('http://renegadeox.com/img/off.png') no-repeat;
height:17px;
padding-left: 18px;
}
.amPmCheckbox input[type="checkbox"]:checked + label {
background: url('http://renegadeox.com/img/on.png') no-repeat;
height:17px;
}

http://jsfiddle.net/JkDhN/1/

Change checkbox image when checked

Since you are not directly clicking on the input you can't use click. What you click on however is the label. The label then changes the input value. So instead of click change it to .. you guessed it, .change()

The next step is to change the image. The problem here is that you changed the src attribute of the input instead of the img element... so first we need to get the img element and change the src.

$("#blackjack").change(function () {

if ($(this).is(':checked'))

$(this).prev().find('img').attr('src', 'http://i.stack.imgur.com/J6dkP.gif');

else

$(this).prev().find('img').attr('src', 'http://i.imgur.com/kfMWC91.jpg');

});
input[type=checkbox] {

display: none;

}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<form method="post" action="recap.php">

<p>Cliquez sur les icônes des jeux que vous souhaitez:

<br>

<p>

<label for="blackjack">

<img src="http://i.imgur.com/kfMWC91.jpg" alt="Sample Image">

</label>

</p>

<INPUT id="blackjack" type="checkbox" value="Blackjack" />

</form>

Dynamically Change Checkbox style to default

If you only want to apply your custom template when the CheckBox is checked you could apply a Style with a Trigger to the CheckBox:

<CheckBox Content="An Image CheckBox Label" Foreground="Black" Margin="73,103,27,142" IsThreeState="True" >
<CheckBox.Style>
<Style TargetType="CheckBox">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<StackPanel Orientation="Horizontal">
<Image Source="/Images/Checkbox.ico" />
<ContentPresenter Content="{TemplateBinding Content}" Margin="5,0,0,0" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>

Changing listView checkBox image

Add ImageList component to form. Add two images to Images property.

Then set that ImageList to StateImageList property on ListView. That two images will be use as checked/unchecked marks.



Related Topics



Leave a reply



Submit