1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.music;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.Checkable;
22 import android.widget.RelativeLayout;
23 
24 /**
25  * A special variation of RelativeLayout that can be used as a checkable object.
26  * This allows it to be used as the top-level view of a list view item, which
27  * also supports checking.  Otherwise, it works identically to a RelativeLayout.
28  */
29 public class CheckableRelativeLayout extends RelativeLayout implements Checkable {
30     private boolean mChecked;
31 
32     private static final int[] CHECKED_STATE_SET = {
33         android.R.attr.state_checked
34     };
35 
CheckableRelativeLayout(Context context, AttributeSet attrs)36     public CheckableRelativeLayout(Context context, AttributeSet attrs) {
37         super(context, attrs);
38     }
39 
40     @Override
onCreateDrawableState(int extraSpace)41     protected int[] onCreateDrawableState(int extraSpace) {
42         final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
43         if (isChecked()) {
44             mergeDrawableStates(drawableState, CHECKED_STATE_SET);
45         }
46         return drawableState;
47     }
48 
toggle()49     public void toggle() {
50         setChecked(!mChecked);
51     }
52 
isChecked()53     public boolean isChecked() {
54         return mChecked;
55     }
56 
setChecked(boolean checked)57     public void setChecked(boolean checked) {
58         if (mChecked != checked) {
59             mChecked = checked;
60             refreshDrawableState();
61         }
62     }
63 }
64