Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (c) 2016, 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 package com.android.car.hvac.ui;
     17 
     18 import android.content.Context;
     19 import android.graphics.drawable.Drawable;
     20 import android.util.AttributeSet;
     21 import android.view.View;
     22 import android.widget.ImageButton;
     23 
     24 /**
     25  * A toggle button that has two states, each with a different drawable icon.
     26  */
     27 public class ToggleButton extends ImageButton {
     28     /**
     29      * A listener that is notified when the button is toggled.
     30      */
     31     public interface ToggleListener {
     32         void onToggled(boolean isOn);
     33     }
     34 
     35     private boolean mIsOn;
     36 
     37     private Drawable mDrawableOff;
     38     private Drawable mDrawableOn;
     39     private ToggleListener mListener;
     40 
     41     public ToggleButton(Context context) {
     42         super(context);
     43     }
     44 
     45     public ToggleButton(Context context, AttributeSet attrs) {
     46         super(context, attrs);
     47     }
     48 
     49     public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
     50         super(context, attrs, defStyleAttr);
     51     }
     52 
     53     @Override
     54     public void onFinishInflate() {
     55         super.onFinishInflate();
     56         setOnClickListener(new OnClickListener() {
     57             @Override
     58             public void onClick(View v) {
     59                 if (mIsOn) {
     60                     setImageDrawable(mDrawableOff);
     61                     if (mListener != null) {
     62                         mListener.onToggled(false);
     63                     }
     64                     mIsOn = false;
     65                 } else {
     66                     setImageDrawable(mDrawableOn);
     67                     if (mListener != null) {
     68                         mListener.onToggled(true);
     69                     }
     70                     mIsOn = true;
     71                 }
     72             }
     73         });
     74     }
     75 
     76     public void setToggleListener(ToggleListener listener) {
     77         mListener = listener;
     78     }
     79 
     80     public void setToggleIcons(Drawable on, Drawable off) {
     81         mDrawableOff = off;
     82         mDrawableOn = on;
     83         setImageDrawable(mIsOn ? mDrawableOn : mDrawableOff);
     84     }
     85 
     86     public void setIsOn(boolean on) {
     87         mIsOn = on;
     88         if (mIsOn) {
     89             setImageDrawable(mDrawableOn);
     90         } else {
     91             setImageDrawable(mDrawableOff);
     92         }
     93     }
     94 }
     95