web-dev-qa-db-fra.com

Comment changer les images par défaut de CheckBox

J'utilise ListView et pour chaque ligne, j'ai row_item.xml et je le gonfle dans le code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:orientation="vertical"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    >
    <CheckBox
        Android:id="@+id/chk"
        Android:layout_alignParentLeft="true"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content" />
    <TextView
        Android:id="@+id/txtChoice"
        Android:textColor="#FF0000"
        Android:text="TEST"
        Android:layout_toRightOf="@id/chk"
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content" /> 
</RelativeLayout>

Comment changer cette checkBox utiliser une autre image custom_1 quand est cochée et une autre image custom_2 quand est décochée?

41
Damir

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" />
141
A.Quiroga

case à cocher est un bouton, vous pouvez donc fournir votre propre dessin avec l'état de décocher de la coche et comme arrière-plan de la case à cocher Par exemple

<?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/yourdrawable1" />
<item Android:state_checked="true" Android:drawable="@drawable/yourdrawable2" />
<item Android:drawable="@drawable/yourdrawable1" /> <!-- default -->
 </selector>

et mettez ceci dans un fichier file.xml dans votre dossier pouvant être dessiné. Dans votre case à cocher:

  <CheckBox
    Android:button="@drawable/file"
    Android:id="@+id/chk"
    Android:layout_alignParentLeft="true"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content" />
9
Blackbelt

C'est très facile :) Vous devez d'abord créer une classe CustomCheckBox qui étendra CheckBox et remplacera la méthode onDraw(Canvas canvas):

public class CustomCheckBox extends CheckBox {
private final Drawable buttonDrawable;

public CustomCheckBox(Context context, AttributeSet set) {
    super(context, set);
    buttonDrawable = getResources().getDrawable(R.drawable.custom_check_box);
    try {
        setButtonDrawable(Android.R.color.transparent);
    } catch (Exception e) {
        // DO NOTHING
    }
    setPadding(10, 5, 50, 5);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    buttonDrawable.setState(getDrawableState());

    final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
    final int height = buttonDrawable.getIntrinsicHeight();
    if (buttonDrawable != null) {
        int y = 0;

        switch (verticalGravity) {
        case Gravity.BOTTOM:
            y = getHeight() - height;
            break;
        case Gravity.CENTER_VERTICAL:
            y = (getHeight() - height) / 2;
            break;
        }

        int buttonWidth = buttonDrawable.getIntrinsicWidth();
        int buttonLeft = getWidth() - buttonWidth - 5;
        buttonDrawable.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y + height);
        buttonDrawable.draw(canvas);
    }
}
}

Créez également votre sélecteur nommé custom_check_box dans votre dossier pouvant être dessiné:

<selector xmlns:Android="http://schemas.Android.com/apk/res/Android">
    <item Android:state_checked="true" Android:state_window_focused="false" 
        Android:drawable="@drawable/btn_check_on" />
    <item Android:state_checked="false" Android:state_window_focused="false"
        Android:drawable="@drawable/btn_check_off" />
    <item Android:state_checked="true" Android:state_pressed="true"
        Android:drawable="@drawable/btn_check_on" />    
    <item Android:state_checked="false" Android:state_focused="true"
        Android:drawable="@drawable/btn_check_off" />
    <item Android:state_checked="false" Android:drawable="@drawable/btn_check_off" />
    <item Android:state_checked="true" Android:drawable="@drawable/btn_check_on" />
</selector>

Et utilisez vos icônes/images personnalisées dans le XML ci-dessus pour les trois états (focalisé/enfoncé/par défaut)

Utilisez le composant personnalisé dans votre code XML comme ceci:

<*package + class path*.CustomCheckBox   // example com.mypackage.ui.CustomCheckBox if your project is named "mypackage" and the class is in the "ui" folder
            Android:text="@string/text"
            Android:checked="false" Android:layout_width="fill_parent"
            Android:id="@+id/myCheckbox" Android:layout_height="wrap_content"/>

et Java:

private CustomCheckBox mCheckbox;
mCheckbox = (CustomCheckBox) findviewbyid(R.id.myCheckbox);

Cela fonctionne parce que je l'ai utilisé dans les deux sens :) Et avec quelques ajustements, cela fonctionne aussi pour RadioButtons de la même manière. Bonne codage!

4
androidu

Vous pouvez utiliser le sélecteur au format xml, qui permet de modifier l’image de la case à cocher de manière dynamique en fonction de son état coché. 

Par exemple:

<item Android:drawable="@drawable/ic_linkedin" Android:state_checked="true" />
<item Android:drawable="@drawable/ic_linkedin_disabled" Android:state_checked="false" />

Dans le fichier suivant, si la case à cocher est cochée, l'icône ic_linkedin est définie et si la case à cocher est décochée, l'icône ic_linkedin_disabled est activée.

0
Hiral Pancholi