Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package android.widget;
 
 
An extension to TextView that supports the Checkable interface. This is useful when used in a ListView where the it's setChoiceMode has been set to something other than CHOICE_MODE_NONE.
 
 public class CheckedTextView extends TextView implements Checkable {
     private boolean mChecked;
     private int mCheckMarkResource;
     private Drawable mCheckMarkDrawable;
     private int mBasePaddingRight;
     private int mCheckMarkWidth;
 
     private static final int[] CHECKED_STATE_SET = {
         ..
     };
 
     public CheckedTextView(Context context) {
         this(contextnull);
     }
 
     public CheckedTextView(Context contextAttributeSet attrs) {
         this(contextattrs, 0);
     }
 
     public CheckedTextView(Context contextAttributeSet attrsint defStyle) {
         super(contextattrsdefStyle);
 
         TypedArray a = context.obtainStyledAttributes(attrs,
                 ..defStyle, 0);
 
         if (d != null) {
             setCheckMarkDrawable(d);
         }
 
         boolean checked = a.getBoolean(..false);
         setChecked(checked);
 
         a.recycle();
     }
 
     public void toggle() {
         setChecked(!);
     }
 
     public boolean isChecked() {
         return ;
     }

    

Changes the checked state of this text view.

Parameters:
checked true to check the text, false to uncheck it
 
     public void setChecked(boolean checked) {
         if ( != checked) {
              = checked;
             refreshDrawableState();
         }
     }


    
Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn when isChecked() is true.

Parameters:
resid The Drawable to use for the checkmark.
    public void setCheckMarkDrawable(int resid) {
        if (resid != 0 && resid == ) {
            return;
        }
         = resid;
        Drawable d = null;
        if ( != 0) {
            d = getResources().getDrawable();
        }
        setCheckMarkDrawable(d);
    }

    
Set the checkmark to a given Drawable. This will be drawn when isChecked() is true.

Parameters:
d The Drawable to use for the checkmark.
    public void setCheckMarkDrawable(Drawable d) {
        if ( != null) {
            .setCallback(null);
            unscheduleDrawable();
        }
        if (d != null) {
            d.setCallback(this);
            d.setVisible(getVisibility() == false);
            d.setState();
            setMinHeight(d.getIntrinsicHeight());
            
             = d.getIntrinsicWidth();
             =  + ;
            d.setState(getDrawableState());
        } else {
             = ;
        }
         = d;
        requestLayout();
    }
    
    @Override
    public void setPadding(int leftint topint rightint bottom) {
        super.setPadding(lefttoprightbottom);
         = ;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        final Drawable checkMarkDrawable = ;
        if (checkMarkDrawable != null) {
            final int verticalGravity = getGravity() & .;
            final int height = checkMarkDrawable.getIntrinsicHeight();
            int y = 0;
            switch (verticalGravity) {
                case .:
                    y = getHeight() - height;
                    break;
                case .:
                    y = (getHeight() - height) / 2;
                    break;
            }
            
            int right = getWidth();
            checkMarkDrawable.setBounds(
                    right -  - 
                    y
                    right - 
                    y + height);
            checkMarkDrawable.draw(canvas);
        }
    }
    
    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState);
        }
        return drawableState;
    }
    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        
        if ( != null) {
            int[] myDrawableState = getDrawableState();
            
            // Set the state of the Drawable
            .setState(myDrawableState);
            
            invalidate();
        }
    }
    @Override
    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
        boolean populated = super.dispatchPopulateAccessibilityEvent(event);
        if (!populated) {
            event.setChecked();
        }
        return populated;
    }
New to GrepCode? Check out our FAQ X