blob: 63525a4a1451eac46eedc93b4092dd42f9789040 [file] [log] [blame]
Eric Erfanian938468d2017-10-24 14:05:52 -07001/*
2 * Copyright (C) 2017 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
17package com.android.newbubble;
18
19import android.content.Context;
20import android.support.v4.view.AccessibilityDelegateCompat;
21import android.support.v4.view.ViewCompat;
22import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
23import android.support.v7.widget.AppCompatButton;
24import android.util.AttributeSet;
25import android.view.View;
26import android.view.accessibility.AccessibilityEvent;
27import android.widget.Checkable;
28
29/**
30 * A {@link android.widget.Button Button} that implements {@link Checkable} and propagates the
31 * checkable state
32 */
33public class NewCheckableButton extends AppCompatButton implements Checkable {
34
35 private boolean mChecked;
36
37 public NewCheckableButton(Context context) {
38 this(context, null);
39 }
40
41 public NewCheckableButton(Context context, AttributeSet attrs) {
42 this(context, attrs, android.R.attr.imageButtonStyle);
43 }
44
45 public NewCheckableButton(Context context, AttributeSet attrs, int defStyleAttr) {
46 super(context, attrs, defStyleAttr);
47
48 ViewCompat.setAccessibilityDelegate(
49 this,
50 new AccessibilityDelegateCompat() {
51 @Override
52 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
53 super.onInitializeAccessibilityEvent(host, event);
54 event.setChecked(isChecked());
55 }
56
57 @Override
58 public void onInitializeAccessibilityNodeInfo(
59 View host, AccessibilityNodeInfoCompat info) {
60 super.onInitializeAccessibilityNodeInfo(host, info);
61 info.setCheckable(true);
62 info.setChecked(isChecked());
63 }
64 });
65 }
66
67 @Override
68 public void setChecked(boolean checked) {
69 if (mChecked != checked) {
70 mChecked = checked;
71 setTextColor(
72 checked
73 ? getContext().getColor(R.color.bubble_button_text_color_blue)
74 : getContext().getColor(R.color.bubble_button_text_color_black));
75 }
76 }
77
78 @Override
79 public boolean isChecked() {
80 return mChecked;
81 }
82
83 @Override
84 public void toggle() {
85 setChecked(!mChecked);
86 }
87}