use of android.text.method.TransformationMethod in project UltimateAndroid by cymcsg.
the class AutofitTextView method refitText.
/**
* Re size the font so the specified text fits in the text box assuming the text box is the
* specified width.
*/
private void refitText() {
if (!mSizeToFit) {
return;
}
if (mMaxLines <= 0) {
// Don't auto-size since there's no limit on lines.
return;
}
CharSequence text = getText();
TransformationMethod method = getTransformationMethod();
if (method != null) {
text = method.getTransformation(text, this);
}
int targetWidth = getWidth() - getPaddingLeft() - getPaddingRight();
if (targetWidth > 0) {
Context context = getContext();
Resources r = Resources.getSystem();
DisplayMetrics displayMetrics;
float size = mMaxTextSize;
float high = size;
float low = 0;
if (context != null) {
r = context.getResources();
}
displayMetrics = r.getDisplayMetrics();
mPaint.set(getPaint());
mPaint.setTextSize(size);
if ((mMaxLines == 1 && mPaint.measureText(text, 0, text.length()) > targetWidth) || getLineCount(text, mPaint, size, targetWidth, displayMetrics) > mMaxLines) {
size = getTextSize(text, mPaint, targetWidth, mMaxLines, low, high, mPrecision, displayMetrics);
}
if (size < mMinTextSize) {
size = mMinTextSize;
}
super.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
}
use of android.text.method.TransformationMethod in project AisenWeiBo by wangdan.
the class TimePickerDialog method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.time_picker_dialog, null);
KeyboardListener keyboardListener = new KeyboardListener();
view.findViewById(R.id.time_picker_dialog).setOnKeyListener(keyboardListener);
Resources res = getResources();
mHourPickerDescription = res.getString(R.string.hour_picker_description);
mSelectHours = res.getString(R.string.select_hours);
mMinutePickerDescription = res.getString(R.string.minute_picker_description);
mSelectMinutes = res.getString(R.string.select_minutes);
mBlue = Utils.resolveColor(getActivity(), R.attr.themeColor, res.getColor(R.color.comm_blue));
mBlack = res.getColor(R.color.numbers_text_color);
mHourView = (TextView) view.findViewById(R.id.hours);
mHourView.setOnKeyListener(keyboardListener);
mHourSpaceView = (TextView) view.findViewById(R.id.hour_space);
mMinuteSpaceView = (TextView) view.findViewById(R.id.minutes_space);
mMinuteView = (TextView) view.findViewById(R.id.minutes);
mMinuteView.setOnKeyListener(keyboardListener);
mAmPmTextView = (TextView) view.findViewById(R.id.ampm_label);
mAmPmTextView.setOnKeyListener(keyboardListener);
if (Build.VERSION.SDK_INT <= 14) {
mAmPmTextView.setTransformationMethod(new TransformationMethod() {
private final Locale locale = getResources().getConfiguration().locale;
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return source != null ? source.toString().toUpperCase(locale) : null;
}
@Override
public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect) {
}
});
}
String[] amPmTexts = new DateFormatSymbols().getAmPmStrings();
mAmText = amPmTexts[0];
mPmText = amPmTexts[1];
mTimePicker = (RadialPickerLayout) view.findViewById(R.id.time_picker);
mTimePicker.setOnValueSelectedListener(this);
mTimePicker.setOnKeyListener(keyboardListener);
mTimePicker.initialize(getActivity(), mInitialHourOfDay, mInitialMinute, mIs24HourMode, mVibrate);
int currentItemShowing = HOUR_INDEX;
if (savedInstanceState != null && savedInstanceState.containsKey(KEY_CURRENT_ITEM_SHOWING)) {
currentItemShowing = savedInstanceState.getInt(KEY_CURRENT_ITEM_SHOWING);
}
setCurrentItemShowing(currentItemShowing, false, true, true);
mTimePicker.invalidate();
mHourView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setCurrentItemShowing(HOUR_INDEX, true, false, true);
mTimePicker.tryVibrate();
}
});
mMinuteView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setCurrentItemShowing(MINUTE_INDEX, true, false, true);
mTimePicker.tryVibrate();
}
});
mDoneButton = (TextView) view.findViewById(R.id.done_button);
mDoneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onDoneButtonClick();
}
});
mDoneButton.setOnKeyListener(keyboardListener);
// Enable or disable the AM/PM view.
mAmPmHitspace = view.findViewById(R.id.ampm_hitspace);
if (mIs24HourMode) {
mAmPmTextView.setVisibility(View.GONE);
RelativeLayout.LayoutParams paramsSeparator = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsSeparator.addRule(RelativeLayout.CENTER_IN_PARENT);
TextView separatorView = (TextView) view.findViewById(R.id.separator);
separatorView.setLayoutParams(paramsSeparator);
} else {
mAmPmTextView.setVisibility(View.VISIBLE);
updateAmPmDisplay(mInitialHourOfDay < 12 ? AM : PM);
mAmPmHitspace.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTimePicker.tryVibrate();
int amOrPm = mTimePicker.getIsCurrentlyAmOrPm();
if (amOrPm == AM) {
amOrPm = PM;
} else if (amOrPm == PM) {
amOrPm = AM;
}
updateAmPmDisplay(amOrPm);
mTimePicker.setAmOrPm(amOrPm);
}
});
}
mAllowAutoAdvance = true;
setHour(mInitialHourOfDay, true);
setMinute(mInitialMinute);
// Set up for keyboard mode.
mDoublePlaceholderText = res.getString(R.string.time_placeholder);
mDeletedKeyFormat = res.getString(R.string.deleted_key);
mPlaceholderText = mDoublePlaceholderText.charAt(0);
mAmKeyCode = mPmKeyCode = -1;
generateLegalTimesTree();
if (mInKbMode) {
mTypedTimes = savedInstanceState.getIntegerArrayList(KEY_TYPED_TIMES);
tryStartingKbMode(-1);
mHourView.invalidate();
} else if (mTypedTimes == null) {
mTypedTimes = new ArrayList<Integer>();
}
return view;
}
use of android.text.method.TransformationMethod in project sbt-android by scala-android.
the class MaterialMultiAutoCompleteTextView method init.
private void init(Context context, AttributeSet attrs) {
iconSize = getPixel(32);
iconOuterWidth = getPixel(48);
iconOuterHeight = getPixel(32);
bottomSpacing = getResources().getDimensionPixelSize(R.dimen.inner_components_spacing);
bottomEllipsisSize = getResources().getDimensionPixelSize(R.dimen.bottom_ellipsis_height);
// default baseColor is black
int defaultBaseColor = Color.BLACK;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText);
textColorStateList = typedArray.getColorStateList(R.styleable.MaterialEditText_met_textColor);
textColorHintStateList = typedArray.getColorStateList(R.styleable.MaterialEditText_met_textColorHint);
baseColor = typedArray.getColor(R.styleable.MaterialEditText_met_baseColor, defaultBaseColor);
// retrieve the default primaryColor
int defaultPrimaryColor;
TypedValue primaryColorTypedValue = new TypedValue();
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
context.getTheme().resolveAttribute(android.R.attr.colorPrimary, primaryColorTypedValue, true);
defaultPrimaryColor = primaryColorTypedValue.data;
} else {
throw new RuntimeException("SDK_INT less than LOLLIPOP");
}
} catch (Exception e) {
try {
int colorPrimaryId = getResources().getIdentifier("colorPrimary", "attr", getContext().getPackageName());
if (colorPrimaryId != 0) {
context.getTheme().resolveAttribute(colorPrimaryId, primaryColorTypedValue, true);
defaultPrimaryColor = primaryColorTypedValue.data;
} else {
throw new RuntimeException("colorPrimary not found");
}
} catch (Exception e1) {
defaultPrimaryColor = baseColor;
}
}
primaryColor = typedArray.getColor(R.styleable.MaterialEditText_met_primaryColor, defaultPrimaryColor);
setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_met_floatingLabel, 0));
errorColor = typedArray.getColor(R.styleable.MaterialEditText_met_errorColor, Color.parseColor("#e7492E"));
minCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_minCharacters, 0);
maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_maxCharacters, 0);
singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_met_singleLineEllipsis, false);
helperText = typedArray.getString(R.styleable.MaterialEditText_met_helperText);
helperTextColor = typedArray.getColor(R.styleable.MaterialEditText_met_helperTextColor, -1);
minBottomTextLines = typedArray.getInt(R.styleable.MaterialEditText_met_minBottomTextLines, 0);
String fontPathForAccent = typedArray.getString(R.styleable.MaterialEditText_met_accentTypeface);
if (fontPathForAccent != null && !isInEditMode()) {
accentTypeface = getCustomTypeface(fontPathForAccent);
textPaint.setTypeface(accentTypeface);
}
String fontPathForView = typedArray.getString(R.styleable.MaterialEditText_met_typeface);
if (fontPathForView != null && !isInEditMode()) {
typeface = getCustomTypeface(fontPathForView);
setTypeface(typeface);
}
floatingLabelText = typedArray.getString(R.styleable.MaterialEditText_met_floatingLabelText);
if (floatingLabelText == null) {
floatingLabelText = getHint();
}
floatingLabelPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_floatingLabelPadding, bottomSpacing);
floatingLabelTextSize = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_floatingLabelTextSize, getResources().getDimensionPixelSize(R.dimen.floating_label_text_size));
floatingLabelTextColor = typedArray.getColor(R.styleable.MaterialEditText_met_floatingLabelTextColor, -1);
floatingLabelAnimating = typedArray.getBoolean(R.styleable.MaterialEditText_met_floatingLabelAnimating, true);
bottomTextSize = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_bottomTextSize, getResources().getDimensionPixelSize(R.dimen.bottom_text_size));
hideUnderline = typedArray.getBoolean(R.styleable.MaterialEditText_met_hideUnderline, false);
underlineColor = typedArray.getColor(R.styleable.MaterialEditText_met_underlineColor, -1);
autoValidate = typedArray.getBoolean(R.styleable.MaterialEditText_met_autoValidate, false);
iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_met_iconLeft, -1));
iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_met_iconRight, -1));
showClearButton = typedArray.getBoolean(R.styleable.MaterialEditText_met_clearButton, false);
clearButtonBitmaps = generateIconBitmaps(R.drawable.met_ic_clear);
iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_iconPadding, getPixel(16));
floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_met_floatingLabelAlwaysShown, false);
helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_met_helperTextAlwaysShown, false);
validateOnFocusLost = typedArray.getBoolean(R.styleable.MaterialEditText_met_validateOnFocusLost, false);
checkCharactersCountAtBeginning = typedArray.getBoolean(R.styleable.MaterialEditText_met_checkCharactersCountAtBeginning, true);
typedArray.recycle();
int[] paddings = new int[] { // 0
android.R.attr.padding, // 1
android.R.attr.paddingLeft, // 2
android.R.attr.paddingTop, // 3
android.R.attr.paddingRight, // 4
android.R.attr.paddingBottom };
TypedArray paddingsTypedArray = context.obtainStyledAttributes(attrs, paddings);
int padding = paddingsTypedArray.getDimensionPixelSize(0, 0);
innerPaddingLeft = paddingsTypedArray.getDimensionPixelSize(1, padding);
innerPaddingTop = paddingsTypedArray.getDimensionPixelSize(2, padding);
innerPaddingRight = paddingsTypedArray.getDimensionPixelSize(3, padding);
innerPaddingBottom = paddingsTypedArray.getDimensionPixelSize(4, padding);
paddingsTypedArray.recycle();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(null);
} else {
setBackgroundDrawable(null);
}
if (singleLineEllipsis) {
TransformationMethod transformationMethod = getTransformationMethod();
setSingleLine();
setTransformationMethod(transformationMethod);
}
initMinBottomLines();
initPadding();
initText();
initFloatingLabel();
initTextWatcher();
checkCharactersCount();
}
use of android.text.method.TransformationMethod in project MaterialEditText by rengwuxian.
the class MaterialAutoCompleteTextView method init.
private void init(Context context, AttributeSet attrs) {
iconSize = getPixel(32);
iconOuterWidth = getPixel(48);
iconOuterHeight = getPixel(32);
bottomSpacing = getResources().getDimensionPixelSize(R.dimen.inner_components_spacing);
bottomEllipsisSize = getResources().getDimensionPixelSize(R.dimen.bottom_ellipsis_height);
// default baseColor is black
int defaultBaseColor = Color.BLACK;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText);
textColorStateList = typedArray.getColorStateList(R.styleable.MaterialEditText_met_textColor);
textColorHintStateList = typedArray.getColorStateList(R.styleable.MaterialEditText_met_textColorHint);
baseColor = typedArray.getColor(R.styleable.MaterialEditText_met_baseColor, defaultBaseColor);
// retrieve the default primaryColor
int defaultPrimaryColor;
TypedValue primaryColorTypedValue = new TypedValue();
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
context.getTheme().resolveAttribute(android.R.attr.colorPrimary, primaryColorTypedValue, true);
defaultPrimaryColor = primaryColorTypedValue.data;
} else {
throw new RuntimeException("SDK_INT less than LOLLIPOP");
}
} catch (Exception e) {
try {
int colorPrimaryId = getResources().getIdentifier("colorPrimary", "attr", getContext().getPackageName());
if (colorPrimaryId != 0) {
context.getTheme().resolveAttribute(colorPrimaryId, primaryColorTypedValue, true);
defaultPrimaryColor = primaryColorTypedValue.data;
} else {
throw new RuntimeException("colorPrimary not found");
}
} catch (Exception e1) {
defaultPrimaryColor = baseColor;
}
}
primaryColor = typedArray.getColor(R.styleable.MaterialEditText_met_primaryColor, defaultPrimaryColor);
setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_met_floatingLabel, 0));
errorColor = typedArray.getColor(R.styleable.MaterialEditText_met_errorColor, Color.parseColor("#e7492E"));
minCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_minCharacters, 0);
maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_met_maxCharacters, 0);
singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_met_singleLineEllipsis, false);
helperText = typedArray.getString(R.styleable.MaterialEditText_met_helperText);
helperTextColor = typedArray.getColor(R.styleable.MaterialEditText_met_helperTextColor, -1);
minBottomTextLines = typedArray.getInt(R.styleable.MaterialEditText_met_minBottomTextLines, 0);
String fontPathForAccent = typedArray.getString(R.styleable.MaterialEditText_met_accentTypeface);
if (fontPathForAccent != null && !isInEditMode()) {
accentTypeface = getCustomTypeface(fontPathForAccent);
textPaint.setTypeface(accentTypeface);
}
String fontPathForView = typedArray.getString(R.styleable.MaterialEditText_met_typeface);
if (fontPathForView != null && !isInEditMode()) {
typeface = getCustomTypeface(fontPathForView);
setTypeface(typeface);
}
floatingLabelText = typedArray.getString(R.styleable.MaterialEditText_met_floatingLabelText);
if (floatingLabelText == null) {
floatingLabelText = getHint();
}
floatingLabelPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_floatingLabelPadding, bottomSpacing);
floatingLabelTextSize = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_floatingLabelTextSize, getResources().getDimensionPixelSize(R.dimen.floating_label_text_size));
floatingLabelTextColor = typedArray.getColor(R.styleable.MaterialEditText_met_floatingLabelTextColor, -1);
floatingLabelAnimating = typedArray.getBoolean(R.styleable.MaterialEditText_met_floatingLabelAnimating, true);
bottomTextSize = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_bottomTextSize, getResources().getDimensionPixelSize(R.dimen.bottom_text_size));
hideUnderline = typedArray.getBoolean(R.styleable.MaterialEditText_met_hideUnderline, false);
underlineColor = typedArray.getColor(R.styleable.MaterialEditText_met_underlineColor, -1);
autoValidate = typedArray.getBoolean(R.styleable.MaterialEditText_met_autoValidate, false);
iconLeftBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_met_iconLeft, -1));
iconRightBitmaps = generateIconBitmaps(typedArray.getResourceId(R.styleable.MaterialEditText_met_iconRight, -1));
showClearButton = typedArray.getBoolean(R.styleable.MaterialEditText_met_clearButton, false);
clearButtonBitmaps = generateIconBitmaps(R.drawable.met_ic_clear);
iconPadding = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_met_iconPadding, getPixel(16));
floatingLabelAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_met_floatingLabelAlwaysShown, false);
helperTextAlwaysShown = typedArray.getBoolean(R.styleable.MaterialEditText_met_helperTextAlwaysShown, false);
validateOnFocusLost = typedArray.getBoolean(R.styleable.MaterialEditText_met_validateOnFocusLost, false);
checkCharactersCountAtBeginning = typedArray.getBoolean(R.styleable.MaterialEditText_met_checkCharactersCountAtBeginning, true);
typedArray.recycle();
int[] paddings = new int[] { // 0
android.R.attr.padding, // 1
android.R.attr.paddingLeft, // 2
android.R.attr.paddingTop, // 3
android.R.attr.paddingRight, // 4
android.R.attr.paddingBottom };
TypedArray paddingsTypedArray = context.obtainStyledAttributes(attrs, paddings);
int padding = paddingsTypedArray.getDimensionPixelSize(0, 0);
innerPaddingLeft = paddingsTypedArray.getDimensionPixelSize(1, padding);
innerPaddingTop = paddingsTypedArray.getDimensionPixelSize(2, padding);
innerPaddingRight = paddingsTypedArray.getDimensionPixelSize(3, padding);
innerPaddingBottom = paddingsTypedArray.getDimensionPixelSize(4, padding);
paddingsTypedArray.recycle();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(null);
} else {
setBackgroundDrawable(null);
}
if (singleLineEllipsis) {
TransformationMethod transformationMethod = getTransformationMethod();
setSingleLine();
setTransformationMethod(transformationMethod);
}
initMinBottomLines();
initPadding();
initText();
initFloatingLabel();
initTextWatcher();
checkCharactersCount();
}
use of android.text.method.TransformationMethod in project android-autofittextview by grantland.
the class AutofitHelper method autofit.
/**
* Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
*/
private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize, int maxLines, float precision) {
if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
// Don't auto-size since there's no limit on lines.
return;
}
int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
if (targetWidth <= 0) {
return;
}
CharSequence text = view.getText();
TransformationMethod method = view.getTransformationMethod();
if (method != null) {
text = method.getTransformation(text, view);
}
Context context = view.getContext();
Resources r = Resources.getSystem();
DisplayMetrics displayMetrics;
float size = maxTextSize;
float high = size;
float low = 0;
if (context != null) {
r = context.getResources();
}
displayMetrics = r.getDisplayMetrics();
paint.set(view.getPaint());
paint.setTextSize(size);
if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth) || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision, displayMetrics);
}
if (size < minTextSize) {
size = minTextSize;
}
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
Aggregations