blob: 15858d39e33084ca2a0a3daa6a3e44f5531d3ed1 [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;
yueg84ac49b2017-11-01 16:22:28 -070020import android.content.res.ColorStateList;
Eric Erfanian938468d2017-10-24 14:05:52 -070021import android.support.v4.view.AccessibilityDelegateCompat;
22import android.support.v4.view.ViewCompat;
23import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
24import android.support.v7.widget.AppCompatButton;
25import android.util.AttributeSet;
26import android.view.View;
27import android.view.accessibility.AccessibilityEvent;
28import android.widget.Checkable;
29
30/**
31 * A {@link android.widget.Button Button} that implements {@link Checkable} and propagates the
32 * checkable state
33 */
34public class NewCheckableButton extends AppCompatButton implements Checkable {
35
linyuh183cb712017-12-27 17:02:37 -080036 private boolean checked;
Eric Erfanian938468d2017-10-24 14:05:52 -070037
38 public NewCheckableButton(Context context) {
39 this(context, null);
40 }
41
42 public NewCheckableButton(Context context, AttributeSet attrs) {
43 this(context, attrs, android.R.attr.imageButtonStyle);
44 }
45
46 public NewCheckableButton(Context context, AttributeSet attrs, int defStyleAttr) {
47 super(context, attrs, defStyleAttr);
48
49 ViewCompat.setAccessibilityDelegate(
50 this,
51 new AccessibilityDelegateCompat() {
52 @Override
53 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
54 super.onInitializeAccessibilityEvent(host, event);
55 event.setChecked(isChecked());
56 }
57
58 @Override
59 public void onInitializeAccessibilityNodeInfo(
60 View host, AccessibilityNodeInfoCompat info) {
61 super.onInitializeAccessibilityNodeInfo(host, info);
62 info.setCheckable(true);
63 info.setChecked(isChecked());
64 }
65 });
66 }
67
68 @Override
69 public void setChecked(boolean checked) {
linyuh183cb712017-12-27 17:02:37 -080070 if (this.checked != checked) {
71 this.checked = checked;
yueg84ac49b2017-11-01 16:22:28 -070072 int newColor =
Eric Erfanian938468d2017-10-24 14:05:52 -070073 checked
yueg84ac49b2017-11-01 16:22:28 -070074 ? getContext().getColor(R.color.bubble_button_color_blue)
75 : getContext().getColor(R.color.bubble_button_color_grey);
76 setTextColor(newColor);
77 setCompoundDrawableTintList(ColorStateList.valueOf(newColor));
Eric Erfanian938468d2017-10-24 14:05:52 -070078 }
79 }
80
81 @Override
82 public boolean isChecked() {
linyuh183cb712017-12-27 17:02:37 -080083 return checked;
Eric Erfanian938468d2017-10-24 14:05:52 -070084 }
85
86 @Override
87 public void toggle() {
linyuh183cb712017-12-27 17:02:37 -080088 setChecked(!checked);
Eric Erfanian938468d2017-10-24 14:05:52 -070089 }
90}