OsmAnd/android/support/v7/internal/widget/CompatTextView.java

85 lines
3 KiB
Java
Raw Normal View History

2015-05-03 14:56:07 +02:00
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.support.v7.internal.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.support.v7.appcompat.R;
import android.support.v7.internal.text.AllCapsTransformationMethod;
import android.text.method.TransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import java.util.Locale;
/**
* @hide
*/
public class CompatTextView extends TextView {
public CompatTextView(Context context) {
this(context, null);
}
public CompatTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CompatTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// First read the TextAppearance style id
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompatTextView,
defStyle, 0);
final int ap = a.getResourceId(R.styleable.CompatTextView_android_textAppearance, -1);
a.recycle();
// Now check TextAppearance's textAllCaps value
if (ap != -1) {
TypedArray appearance = context.obtainStyledAttributes(ap, R.styleable.TextAppearance);
if (appearance.hasValue(R.styleable.TextAppearance_textAllCaps)) {
setAllCaps(appearance.getBoolean(R.styleable.TextAppearance_textAllCaps, false));
}
appearance.recycle();
}
// Now read the style's value
a = context.obtainStyledAttributes(attrs, R.styleable.CompatTextView, defStyle, 0);
if (a.hasValue(R.styleable.CompatTextView_textAllCaps)) {
setAllCaps(a.getBoolean(R.styleable.CompatTextView_textAllCaps, false));
}
a.recycle();
}
public void setAllCaps(boolean allCaps) {
setTransformationMethod(allCaps ? new AllCapsTransformationMethod(getContext()) : null);
}
@Override
public void setTextAppearance(Context context, int resid) {
super.setTextAppearance(context, resid);
TypedArray appearance = context.obtainStyledAttributes(resid, R.styleable.TextAppearance);
if (appearance.hasValue(R.styleable.TextAppearance_textAllCaps)) {
setAllCaps(appearance.getBoolean(R.styleable.TextAppearance_textAllCaps, false));
}
appearance.recycle();
}
}