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