jeudi 16 juillet 2015

Custom checkbox class in android

I needed to correlate checkboxes with their position in the list view in the checkboxes' onClickListener. My first solution was to make a very short custom view that extended checkbox but had an extra variable (position) that would be set in getView().

    public class ListCheckBox extends CheckBox {
    private int position = -1;

   public ListCheckBox(Context context)
   {super(context);}
   public ListCheckBox(Context context, AttributeSet attrs)
   {super(context,attrs);}
   public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr)
   {super(context,attrs,defStyleAttr);}
    @TargetApi(21)
    public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
   {super(context,attrs,defStyleAttr,defStyleRes);}

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }
}

It worked, except it changed the checkboxes' color to black, and nothing I did (including changing android:buttonTint) could change it. For now my solution is a HashMap with a view key and integer value that keeps track of the checkboxes' and their positions, but if anyone has a less ugly solution or any idea as to why I couldn't change the color of the checkboxes, it would be very much appreciated.




Aucun commentaire:

Enregistrer un commentaire