Home | History | Annotate | Download | only in inflater
      1 /*
      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 
     17 package androidx.appcompat.app.inflater;
     18 
     19 import android.content.Context;
     20 import android.util.AttributeSet;
     21 import android.view.View;
     22 import android.widget.ToggleButton;
     23 
     24 import androidx.annotation.NonNull;
     25 import androidx.annotation.Nullable;
     26 import androidx.appcompat.app.AppCompatViewInflater;
     27 import androidx.appcompat.widget.AppCompatButton;
     28 import androidx.appcompat.widget.AppCompatImageButton;
     29 import androidx.appcompat.widget.AppCompatTextView;
     30 
     31 /**
     32  * Custom view inflater that takes over the inflation of a few widget types.
     33  */
     34 public class CustomViewInflater extends AppCompatViewInflater {
     35     public static class CustomTextView extends AppCompatTextView {
     36         public CustomTextView(Context context) {
     37             super(context);
     38         }
     39 
     40         public CustomTextView(Context context,
     41                 @Nullable AttributeSet attrs) {
     42             super(context, attrs);
     43         }
     44 
     45         public CustomTextView(Context context,
     46                 @Nullable AttributeSet attrs, int defStyleAttr) {
     47             super(context, attrs, defStyleAttr);
     48         }
     49     }
     50 
     51     public static class CustomButton extends AppCompatButton {
     52         public CustomButton(Context context) {
     53             super(context);
     54         }
     55 
     56         public CustomButton(Context context, AttributeSet attrs) {
     57             super(context, attrs);
     58         }
     59 
     60         public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
     61             super(context, attrs, defStyleAttr);
     62         }
     63     }
     64 
     65     public static class CustomImageButton extends AppCompatImageButton {
     66         public CustomImageButton(Context context) {
     67             super(context);
     68         }
     69 
     70         public CustomImageButton(Context context, AttributeSet attrs) {
     71             super(context, attrs);
     72         }
     73 
     74         public CustomImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
     75             super(context, attrs, defStyleAttr);
     76         }
     77     }
     78 
     79     public static class CustomToggleButton extends ToggleButton {
     80         public CustomToggleButton(Context context, AttributeSet attrs, int defStyleAttr,
     81                 int defStyleRes) {
     82             super(context, attrs, defStyleAttr, defStyleRes);
     83         }
     84 
     85         public CustomToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
     86             super(context, attrs, defStyleAttr);
     87         }
     88 
     89         public CustomToggleButton(Context context, AttributeSet attrs) {
     90             super(context, attrs);
     91         }
     92 
     93         public CustomToggleButton(Context context) {
     94             super(context);
     95         }
     96     }
     97 
     98     @NonNull
     99     @Override
    100     protected AppCompatButton createButton(Context context, AttributeSet attrs) {
    101         return new CustomButton(context, attrs);
    102     }
    103 
    104     @NonNull
    105     @Override
    106     protected AppCompatTextView createTextView(Context context, AttributeSet attrs) {
    107         return new CustomTextView(context, attrs);
    108     }
    109 
    110     @NonNull
    111     @Override
    112     protected AppCompatImageButton createImageButton(Context context, AttributeSet attrs) {
    113         return new CustomImageButton(context, attrs);
    114     }
    115 
    116     @Nullable
    117     @Override
    118     protected View createView(Context context, String name, AttributeSet attrs) {
    119         if (name.equals("ToggleButton")) {
    120             return new CustomToggleButton(context, attrs);
    121         }
    122         return null;
    123     }
    124 }
    125