blob: 125fecb5d13d8187f1f5d8965a652e3466b3d4a1 [file] [log] [blame]
The Android Open Source Project792a2202009-03-03 19:32:30 -08001/*
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
Jack Hef02d3c62017-02-21 00:39:22 -050017package com.android.music.utils;
The Android Open Source Project792a2202009-03-03 19:32:30 -080018
19import android.content.Context;
20import android.util.AttributeSet;
21import android.widget.Checkable;
22import 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 */
29public class CheckableRelativeLayout extends RelativeLayout implements Checkable {
30 private boolean mChecked;
31
Jack Heb0fba8b2017-01-26 15:54:38 -080032 private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
The Android Open Source Project792a2202009-03-03 19:32:30 -080033
34 public CheckableRelativeLayout(Context context, AttributeSet attrs) {
35 super(context, attrs);
36 }
37
38 @Override
39 protected int[] onCreateDrawableState(int extraSpace) {
40 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
41 if (isChecked()) {
42 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
43 }
44 return drawableState;
45 }
46
47 public void toggle() {
48 setChecked(!mChecked);
49 }
Jack Heb0fba8b2017-01-26 15:54:38 -080050
The Android Open Source Project792a2202009-03-03 19:32:30 -080051 public boolean isChecked() {
52 return mChecked;
53 }
54
55 public void setChecked(boolean checked) {
56 if (mChecked != checked) {
57 mChecked = checked;
58 refreshDrawableState();
59 }
60 }
61}