|
| 1 | +package com.neopixl.pixlui.components.radiobutton; |
| 2 | + |
| 3 | +import com.android.export.AllCapsTransformationMethod; |
| 4 | +import com.neopixl.pixlui.components.textview.FontFactory; |
| 5 | +import com.neopixl.pixlui.intern.PixlUIContants; |
| 6 | + |
| 7 | +import android.annotation.SuppressLint; |
| 8 | +import android.content.Context; |
| 9 | +import android.graphics.Paint; |
| 10 | +import android.graphics.Typeface; |
| 11 | +import android.util.AttributeSet; |
| 12 | + |
| 13 | +/* |
| 14 | + Copyright 2014 Olivier Demolliens |
| 15 | +
|
| 16 | + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this |
| 17 | +
|
| 18 | + file except in compliance with the License. You may obtain a copy of the License at |
| 19 | +
|
| 20 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | +
|
| 22 | + Unless required by applicable law or agreed to in writing, software distributed under |
| 23 | +
|
| 24 | + the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 25 | +
|
| 26 | + ANY KIND, either express or implied. See the License for the specific language governing |
| 27 | +
|
| 28 | + permissions and limitations under the License. |
| 29 | + */ |
| 30 | +public class RadioButton extends android.widget.RadioButton { |
| 31 | + |
| 32 | + /** |
| 33 | + * XML attribute |
| 34 | + */ |
| 35 | + private static String RADIOBUTTON_ATTRIBUTE_FONT_NAME = "typeface"; |
| 36 | + private static final String RADIOBUTTON_OS_ATTRIBUTE_TEXT_ALL_CAPS = "textAllCaps"; |
| 37 | + |
| 38 | + /** |
| 39 | + * State |
| 40 | + */ |
| 41 | + private boolean mOldDeviceTextAllCaps; |
| 42 | + |
| 43 | + public RadioButton(Context context) { |
| 44 | + super(context); |
| 45 | + radioButtonVersion(); |
| 46 | + } |
| 47 | + |
| 48 | + public RadioButton(Context context, AttributeSet attrs) { |
| 49 | + super(context, attrs); |
| 50 | + radioButtonVersion(); |
| 51 | + setCustomFont(context, attrs); |
| 52 | + if (isOldDeviceTextAllCaps()) { |
| 53 | + setAllCaps(context, attrs); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public RadioButton(Context context, AttributeSet attrs, int defStyle) { |
| 58 | + super(context, attrs, defStyle); |
| 59 | + radioButtonVersion(); |
| 60 | + setCustomFont(context, attrs); |
| 61 | + if (isOldDeviceTextAllCaps()) { |
| 62 | + setAllCaps(context, attrs); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Define what version of code we need to use |
| 68 | + */ |
| 69 | + private void radioButtonVersion() { |
| 70 | + if (android.os.Build.VERSION.SDK_INT < 14) { |
| 71 | + setOldDeviceTextAllCaps(true); |
| 72 | + } else { |
| 73 | + setOldDeviceTextAllCaps(false); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * XML methods |
| 79 | + * |
| 80 | + * @param ctx |
| 81 | + * @param attrs |
| 82 | + */ |
| 83 | + private void setCustomFont(Context ctx, AttributeSet attrs) { |
| 84 | + |
| 85 | + String typefaceName = attrs.getAttributeValue( |
| 86 | + PixlUIContants.SCHEMA_URL, RADIOBUTTON_ATTRIBUTE_FONT_NAME); |
| 87 | + |
| 88 | + if (typefaceName != null) { |
| 89 | + setPaintFlags(this.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG |
| 90 | + | Paint.LINEAR_TEXT_FLAG); |
| 91 | + setCustomFont(ctx, typefaceName); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * XML methods |
| 97 | + * |
| 98 | + * @param ctx |
| 99 | + * @param attrs |
| 100 | + */ |
| 101 | + @SuppressLint("NewApi") |
| 102 | + private void setAllCaps(Context ctx, AttributeSet attrs) { |
| 103 | + |
| 104 | + if (!isInEditMode()) { |
| 105 | + int indexSize = attrs.getAttributeCount(); |
| 106 | + |
| 107 | + boolean allCaps = false; |
| 108 | + |
| 109 | + for (int i = 0; i < indexSize; i++) { |
| 110 | + if (attrs.getAttributeName(i).equals( |
| 111 | + RADIOBUTTON_OS_ATTRIBUTE_TEXT_ALL_CAPS)) { |
| 112 | + allCaps = attrs.getAttributeBooleanValue(i, false); |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (allCaps) { |
| 118 | + setAllCaps(allCaps); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Use this method to uppercase all char in text. |
| 125 | + * |
| 126 | + * @param allCaps |
| 127 | + */ |
| 128 | + @SuppressLint("NewApi") |
| 129 | + public void setAllCaps(boolean allCaps) { |
| 130 | + if (this.isOldDeviceTextAllCaps()) { |
| 131 | + if (allCaps) { |
| 132 | + setTransformationMethod(new AllCapsTransformationMethod( |
| 133 | + getContext())); |
| 134 | + } else { |
| 135 | + setTransformationMethod(null); |
| 136 | + } |
| 137 | + } else { |
| 138 | + super.setAllCaps(allCaps); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Use this method to set a custom font in your code (/assets/fonts/) |
| 144 | + * |
| 145 | + * @param ctx |
| 146 | + * @param Font |
| 147 | + * Name, don't forget to add file extension |
| 148 | + * @return |
| 149 | + */ |
| 150 | + public boolean setCustomFont(Context ctx, String font) { |
| 151 | + Typeface tf = FontFactory.getInstance(ctx).getFont(font); |
| 152 | + if (tf != null) { |
| 153 | + setTypeface(tf); |
| 154 | + return true; |
| 155 | + } else { |
| 156 | + return false; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + public boolean isOldDeviceTextAllCaps() { |
| 161 | + return mOldDeviceTextAllCaps; |
| 162 | + } |
| 163 | + |
| 164 | + public void setOldDeviceTextAllCaps(boolean mOldDeviceTextAllCaps) { |
| 165 | + this.mOldDeviceTextAllCaps = mOldDeviceTextAllCaps; |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments