TextViewCompat.java revision d4bea8be0627dac53fb5907f1f64dddf6147d55e
1/*
2 * Copyright (C) 2015 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 android.support.v4.widget;
18
19import android.graphics.drawable.Drawable;
20import android.os.Build;
21import android.support.annotation.NonNull;
22import android.support.annotation.Nullable;
23import android.widget.TextView;
24
25/**
26 * Helper for accessing features in {@link TextView} introduced after API level
27 * 4 in a backwards compatible fashion.
28 */
29public class TextViewCompat {
30
31    // Hide constructor
32    private TextViewCompat() {
33    }
34
35    interface TextViewCompatImpl {
36
37        public void setCompoundDrawablesRelative(@NonNull TextView textView,
38                @Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end,
39                @Nullable Drawable bottom);
40
41        public void setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull TextView textView,
42                @Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end,
43                @Nullable Drawable bottom);
44
45        public void setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull TextView textView,
46                int start, int top, int end, int bottom);
47
48    }
49
50    static class BaseTextViewCompatImpl implements TextViewCompatImpl {
51
52        @Override
53        public void setCompoundDrawablesRelative(@NonNull TextView textView,
54                @Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end,
55                @Nullable Drawable bottom) {
56            textView.setCompoundDrawables(start, top, end, bottom);
57        }
58
59        @Override
60        public void setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull TextView textView,
61                @Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end,
62                @Nullable Drawable bottom) {
63            textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
64        }
65
66        @Override
67        public void setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull TextView textView,
68                int start, int top, int end, int bottom) {
69            textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
70        }
71
72    }
73
74    static class JbMr1TextViewCompatImpl extends BaseTextViewCompatImpl {
75
76        @Override
77        public void setCompoundDrawablesRelative(@NonNull TextView textView,
78                @Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end,
79                @Nullable Drawable bottom) {
80            TextViewCompatJbMr1.setCompoundDrawablesRelative(textView, start, top, end, bottom);
81        }
82
83        @Override
84        public void setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull TextView textView,
85                @Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end,
86                @Nullable Drawable bottom) {
87            TextViewCompatJbMr1.setCompoundDrawablesRelativeWithIntrinsicBounds(textView,
88                    start, top, end, bottom);
89        }
90
91        @Override
92        public void setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull TextView textView,
93                int start, int top, int end, int bottom) {
94            TextViewCompatJbMr1.setCompoundDrawablesRelativeWithIntrinsicBounds(textView,
95                    start, top, end, bottom);
96        }
97
98    }
99
100    static class JbMr2TextViewCompatImpl extends JbMr1TextViewCompatImpl {
101
102        @Override
103        public void setCompoundDrawablesRelative(@NonNull TextView textView,
104                @Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end,
105                @Nullable Drawable bottom) {
106            TextViewCompatJbMr2.setCompoundDrawablesRelative(textView, start, top, end, bottom);
107        }
108
109        @Override
110        public void setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull TextView textView,
111                @Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end,
112                @Nullable Drawable bottom) {
113            TextViewCompatJbMr2
114                    .setCompoundDrawablesRelativeWithIntrinsicBounds(textView, start, top, end,
115                            bottom);
116        }
117
118        @Override
119        public void setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull TextView textView,
120                int start, int top, int end, int bottom) {
121            TextViewCompatJbMr2.setCompoundDrawablesRelativeWithIntrinsicBounds(textView,
122                    start, top, end, bottom);
123        }
124    }
125
126    static final TextViewCompatImpl IMPL;
127
128    static {
129        final int version = Build.VERSION.SDK_INT;
130        if (version >= 18) {
131            IMPL = new JbMr2TextViewCompatImpl();
132        } else if (version >= 17) {
133            IMPL = new JbMr1TextViewCompatImpl();
134        } else {
135            IMPL = new BaseTextViewCompatImpl();
136        }
137    }
138
139    /**
140     * Sets the Drawables (if any) to appear to the start of, above, to the end
141     * of, and below the text. Use {@code null} if you do not want a Drawable
142     * there. The Drawables must already have had {@link Drawable#setBounds}
143     * called.
144     * <p/>
145     * Calling this method will overwrite any Drawables previously set using
146     * {@link TextView#setCompoundDrawables} or related methods.
147     *
148     * @param textView The TextView against which to invoke the method.
149     * @attr ref android.R.styleable#TextView_drawableStart
150     * @attr ref android.R.styleable#TextView_drawableTop
151     * @attr ref android.R.styleable#TextView_drawableEnd
152     * @attr ref android.R.styleable#TextView_drawableBottom
153     */
154    public static void setCompoundDrawablesRelative(@NonNull TextView textView,
155            @Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end,
156            @Nullable Drawable bottom) {
157        IMPL.setCompoundDrawablesRelative(textView, start, top, end, bottom);
158    }
159
160    /**
161     * Sets the Drawables (if any) to appear to the start of, above, to the end
162     * of, and below the text. Use {@code null} if you do not want a Drawable
163     * there. The Drawables' bounds will be set to their intrinsic bounds.
164     * <p/>
165     * Calling this method will overwrite any Drawables previously set using
166     * {@link TextView#setCompoundDrawables} or related methods.
167     *
168     * @param textView The TextView against which to invoke the method.
169     * @attr ref android.R.styleable#TextView_drawableStart
170     * @attr ref android.R.styleable#TextView_drawableTop
171     * @attr ref android.R.styleable#TextView_drawableEnd
172     * @attr ref android.R.styleable#TextView_drawableBottom
173     */
174    public static void setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull TextView textView,
175            @Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end,
176            @Nullable Drawable bottom) {
177        IMPL.setCompoundDrawablesRelativeWithIntrinsicBounds(textView, start, top, end, bottom);
178    }
179
180    /**
181     * Sets the Drawables (if any) to appear to the start of, above, to the end
182     * of, and below the text. Use 0 if you do not want a Drawable there. The
183     * Drawables' bounds will be set to their intrinsic bounds.
184     * <p/>
185     * Calling this method will overwrite any Drawables previously set using
186     * {@link TextView#setCompoundDrawables} or related methods.
187     *
188     * @param textView The TextView against which to invoke the method.
189     * @param start    Resource identifier of the start Drawable.
190     * @param top      Resource identifier of the top Drawable.
191     * @param end      Resource identifier of the end Drawable.
192     * @param bottom   Resource identifier of the bottom Drawable.
193     * @attr ref android.R.styleable#TextView_drawableStart
194     * @attr ref android.R.styleable#TextView_drawableTop
195     * @attr ref android.R.styleable#TextView_drawableEnd
196     * @attr ref android.R.styleable#TextView_drawableBottom
197     */
198    public static void setCompoundDrawablesRelativeWithIntrinsicBounds(@NonNull TextView textView,
199            int start, int top, int end, int bottom) {
200        IMPL.setCompoundDrawablesRelativeWithIntrinsicBounds(textView, start, top, end, bottom);
201    }
202
203}
204