android.text.method.TransformationMethod Java Examples

The following examples show how to use android.text.method.TransformationMethod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextInputLayoutActions.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Sets the transformation method. */
public static ViewAction setTransformationMethod(
    final TransformationMethod transformationMethod) {
  return new ViewAction() {

    @Override
    public Matcher<View> getConstraints() {
      return ViewMatchers.isAssignableFrom(TextInputLayout.class);
    }

    @Override
    public String getDescription() {
      return "Sets the transformation method";
    }

    @Override
    public void perform(UiController uiController, View view) {
      TextInputLayout item = (TextInputLayout) view;
      item.getEditText().setTransformationMethod(transformationMethod);
    }
  };
}
 
Example #2
Source File: AutofitHelper.java    From stynico with MIT License 6 votes vote down vote up
private static int getMaxLines(AppCompatTextView view)
{
       int maxLines = -1; // No limit (Integer.MAX_VALUE also means no limit)

       TransformationMethod method = view.getTransformationMethod();
       if (method != null && method instanceof SingleLineTransformationMethod)
	{
           maxLines = 1;
       }
       else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
	{
           // setMaxLines() and getMaxLines() are only available on android-16+
           maxLines = view.getMaxLines();
       }

       return maxLines;
   }
 
Example #3
Source File: AutofitHelper.java    From kAndroid with Apache License 2.0 5 votes vote down vote up
private static int getMaxLines(TextView view) {
    int maxLines = -1; // No limit (Integer.MAX_VALUE also means no limit)

    TransformationMethod method = view.getTransformationMethod();
    if (method != null && method instanceof SingleLineTransformationMethod) {
        maxLines = 1;
    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // setMaxLines() and getMaxLines() are only available on android-16+
        maxLines = view.getMaxLines();
    }

    return maxLines;
}
 
Example #4
Source File: CardEditTextTest.java    From android-card-form with MIT License 5 votes vote down vote up
@Test
public void doesNotMaskIfFieldIsInvalid() {
    TransformationMethod originalMethod = mView.getTransformationMethod();

    mView.setMask(true);
    type("4111111111111112");

    assertEquals(originalMethod, mView.getTransformationMethod());
}
 
Example #5
Source File: CardEditTextTest.java    From android-card-form with MIT License 5 votes vote down vote up
@Test
public void doesNotMaskIfMaskingIsNotEnabled() {
    TransformationMethod originalMethod = mView.getTransformationMethod();

    type("4111111111111111");
    mView.onFocusChanged(false, 1, null);

    assertEquals(originalMethod, mView.getTransformationMethod());
}
 
Example #6
Source File: AutoExpandTextView.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Re size the font so the specified text fits in the text box assuming the text box is the
 * specified width.
 */
private void refitText() {
    CharSequence text = getText();

    if (TextUtils.isEmpty(text)) {
        return;
    }

    TransformationMethod method = getTransformationMethod();
    if (method != null) {
        text = method.getTransformation(text, this);
    }
    int targetWidth = getWidth() - getPaddingLeft() - getPaddingRight();
    if (targetWidth > 0) {
        float high = 100;
        float low = 0;

        mPaint.set(getPaint());
        mPaint.setTextSize(getTextSize());
        float letterSpacing = getLetterSpacing(text, mPaint, targetWidth, low, high,
                mPrecision);
        mPaint.setLetterSpacing(letterSpacing);
        calculateSections(text);

        super.setLetterSpacing(letterSpacing);
    }
}
 
Example #7
Source File: TextInputLayoutIconsTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private static ViewAssertion isPasswordToggledVisible(final boolean isToggledVisible) {
  return (view, noViewFoundException) -> {
    assertTrue(view instanceof TextInputLayout);
    EditText editText = ((TextInputLayout) view).getEditText();
    TransformationMethod transformationMethod = editText.getTransformationMethod();
    if (isToggledVisible) {
      assertNull(transformationMethod);
    } else {
      assertEquals(PasswordTransformationMethod.getInstance(), transformationMethod);
    }
  };
}
 
Example #8
Source File: AutofitHelper.java    From DarkCalculator with MIT License 5 votes vote down vote up
private static int getMaxLines(TextView view) {
    int maxLines = -1; // No limit (Integer.MAX_VALUE also means no limit)

    TransformationMethod method = view.getTransformationMethod();
    if (method != null && method instanceof SingleLineTransformationMethod) {
        maxLines = 1;
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // setMaxLines() and getMaxLines() are only available on android-16+
        maxLines = view.getMaxLines();
    }

    return maxLines;
}
 
Example #9
Source File: TextUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
static int getMaxLines(TextView view) {
    int maxLines = -1; // No limit (Integer.MAX_VALUE also means no limit)

    TransformationMethod method = view.getTransformationMethod();
    if (method != null && method instanceof SingleLineTransformationMethod) {
        maxLines = 1;
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // setMaxLines() and getMaxLines() are only available on android-16+
        maxLines = view.getMaxLines();
    }

    return maxLines;
}
 
Example #10
Source File: TextViewUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取文本视图显示转换
 * @param textView {@link TextView}
 * @param <T>      泛型
 * @return {@link TransformationMethod}
 */
public static <T extends TextView> TransformationMethod getTransformationMethod(final T textView) {
    if (textView != null) {
        return textView.getTransformationMethod();
    }
    return null;
}
 
Example #11
Source File: ViewHelper.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 设置文本视图显示转换
 * @param view   {@link View}
 * @param method {@link TransformationMethod}
 * @return {@link ViewHelper}
 */
public ViewHelper setTransformationMethod(final View view, final TransformationMethod method) {
    if (view instanceof EditText) {
        EditTextUtils.setTransformationMethod(EditTextUtils.getEditText(view), method);
    } else {
        TextViewUtils.setTransformationMethod(view, method);
    }
    return this;
}
 
Example #12
Source File: EditTextUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取文本视图显示转换
 * @param editText {@link EditText}
 * @param <T>      泛型
 * @return {@link TransformationMethod}
 */
public static <T extends EditText> TransformationMethod getTransformationMethod(final T editText) {
    if (editText != null) {
        return editText.getTransformationMethod();
    }
    return null;
}
 
Example #13
Source File: AutofitTextView.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example #14
Source File: TextUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
static void adjustTextSize(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);
}
 
Example #15
Source File: AutofitTextView.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example #16
Source File: AutofitTextView.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example #17
Source File: EditText.java    From AndroidMaterialValidation with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the transformation that is applied to the text that this TextView is displaying.
 */
public final void setTransformationMethod(final TransformationMethod method) {
    getView().setTransformationMethod(method);
}
 
Example #18
Source File: MaterialEditText.java    From Mover with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MaterialEditText(Context context, AttributeSet attrs, int style) {
    super(context, attrs, style);

    setFocusable(true);
    setFocusableInTouchMode(true);
    setClickable(true);

    floatingLabelTextSize = getResources().getDimensionPixelSize(R.dimen.mtrl_ed_floating_label_text_size);
    bottomSpacing = getResources().getDimensionPixelSize(R.dimen.mtrl_ed_inner_components_spacing);
    bottomEllipsisSize = getResources().getDimensionPixelSize(R.dimen.mtrl_ed_bottom_ellipsis_height);


    // retrieve the default baseColor
    int defaultBaseColor;
    TypedValue baseColorTypedValue = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.windowBackground, baseColorTypedValue, true);
    defaultBaseColor = ColorUtils.getBaseColor(baseColorTypedValue.data);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText);
    baseColor = typedArray.getColor(R.styleable.MaterialEditText_baseColor, defaultBaseColor);
    ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, EMPTY_STATE_SET}, new int[]{baseColor & 0x00ffffff | 0xdf000000, baseColor & 0x00ffffff | 0x44000000});
    setTextColor(colorStateList);

    // retrieve the default primaryColor
    int defaultPrimaryColor;
    TypedValue primaryColorTypedValue = new TypedValue();
    try {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            throw new RuntimeException("SDK_INT less than LOLLIPOP");
        }
        context.getTheme().resolveAttribute(android.R.attr.colorPrimary, primaryColorTypedValue, true);
        defaultPrimaryColor = primaryColorTypedValue.data;
    } catch (Exception e) {
        try {
            int colorAccentId = getResources().getIdentifier("colorPrimary", "attr", getContext().getPackageName());
            if (colorAccentId != 0) {
                context.getTheme().resolveAttribute(colorAccentId, primaryColorTypedValue, true);
                defaultPrimaryColor = primaryColorTypedValue.data;
            } else {
                throw new RuntimeException("colorAccent not found");
            }
        } catch (Exception e1) {
            defaultPrimaryColor = baseColor;
        }
    }

    primaryColor = typedArray.getColor(R.styleable.MaterialEditText_primaryColor, defaultPrimaryColor);
    setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_floatingLabel, 0));
    errorColor = typedArray.getColor(R.styleable.MaterialEditText_errorColor, Color.parseColor("#e7492E"));
    minCharacters = typedArray.getInt(R.styleable.MaterialEditText_minCharacters, 0);
    maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_maxCharacters, 0);
    singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_singleLineEllipsis, false);
    helperText = typedArray.getString(R.styleable.MaterialEditText_helperText);
    helperTextColor = typedArray.getColor(R.styleable.MaterialEditText_helperTextColor, -1);
    minBottomTextLines = typedArray.getInt(R.styleable.MaterialEditText_minBottomTextLines, 0);
    String fontPath = typedArray.getString(R.styleable.MaterialEditText_accentTypeface);
    if (fontPath != null) {
        accentTypeface = getCustomTypeface(fontPath);
        textPaint.setTypeface(accentTypeface);
    }
    floatingLabelText = typedArray.getString(R.styleable.MaterialEditText_floatingLabelText);
    if (floatingLabelText == null) {
        floatingLabelText = getHint();
    }
    floatingLabelSpacing = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_floatingLabelSpacing, bottomSpacing);
    hideUnderline = typedArray.getBoolean(R.styleable.MaterialEditText_hideUnderline, false);
    typedArray.recycle();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        setBackground(null);
    } else {
        setBackgroundDrawable(null);
    }
    if (singleLineEllipsis) {
        TransformationMethod transformationMethod = getTransformationMethod();
        setSingleLine();
        setTransformationMethod(transformationMethod);
    }
    textPaint.setTextSize(floatingLabelTextSize);
    fontMetrics = textPaint.getFontMetrics();
    initMinBottomLines();
    initPadding();
    initText();
    initFloatingLabel();
    initErrorTextListener();
}
 
Example #19
Source File: MaterialAutoCompleteTextView.java    From Mover with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MaterialAutoCompleteTextView(Context context, AttributeSet attrs, int style) {
    super(context, attrs, style);

    setFocusable(true);
    setFocusableInTouchMode(true);
    setClickable(true);

    floatingLabelTextSize = getResources().getDimensionPixelSize(R.dimen.mtrl_ed_floating_label_text_size);
    bottomSpacing = getResources().getDimensionPixelSize(R.dimen.mtrl_ed_inner_components_spacing);
    bottomEllipsisSize = getResources().getDimensionPixelSize(R.dimen.mtrl_ed_bottom_ellipsis_height);


    // retrieve the default baseColor
    int defaultBaseColor;
    TypedValue baseColorTypedValue = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.windowBackground, baseColorTypedValue, true);
    defaultBaseColor = ColorUtils.getBaseColor(baseColorTypedValue.data);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText);
    baseColor = typedArray.getColor(R.styleable.MaterialEditText_baseColor, defaultBaseColor);
    ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, EMPTY_STATE_SET}, new int[]{baseColor & 0x00ffffff | 0xdf000000, baseColor & 0x00ffffff | 0x44000000});
    setTextColor(colorStateList);

    // retrieve the default primaryColor
    int defaultPrimaryColor;
    TypedValue primaryColorTypedValue = new TypedValue();
    try {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            throw new RuntimeException("SDK_INT less than LOLLIPOP");
        }
        context.getTheme().resolveAttribute(android.R.attr.colorPrimary, primaryColorTypedValue, true);
        defaultPrimaryColor = primaryColorTypedValue.data;
    } catch (Exception e) {
        try {
            int colorAccentId = getResources().getIdentifier("colorPrimary", "attr", getContext().getPackageName());
            if (colorAccentId != 0) {
                context.getTheme().resolveAttribute(colorAccentId, primaryColorTypedValue, true);
                defaultPrimaryColor = primaryColorTypedValue.data;
            } else {
                throw new RuntimeException("colorAccent not found");
            }
        } catch (Exception e1) {
            defaultPrimaryColor = baseColor;
        }
    }

    primaryColor = typedArray.getColor(R.styleable.MaterialEditText_primaryColor, defaultPrimaryColor);
    setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_floatingLabel, 0));
    errorColor = typedArray.getColor(R.styleable.MaterialEditText_errorColor, Color.parseColor("#e7492E"));
    minCharacters = typedArray.getInt(R.styleable.MaterialEditText_minCharacters, 0);
    maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_maxCharacters, 0);
    singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_singleLineEllipsis, false);
    helperText = typedArray.getString(R.styleable.MaterialEditText_helperText);
    helperTextColor = typedArray.getColor(R.styleable.MaterialEditText_helperTextColor, -1);
    minBottomTextLines = typedArray.getInt(R.styleable.MaterialEditText_minBottomTextLines, 0);
    String fontPath = typedArray.getString(R.styleable.MaterialEditText_accentTypeface);
    if (fontPath != null) {
        accentTypeface = getCustomTypeface(fontPath);
        textPaint.setTypeface(accentTypeface);
    }
    floatingLabelText = typedArray.getString(R.styleable.MaterialEditText_floatingLabelText);
    if (floatingLabelText == null) {
        floatingLabelText = getHint();
    }
    floatingLabelSpacing = typedArray.getDimensionPixelSize(R.styleable.MaterialEditText_floatingLabelSpacing, bottomSpacing);
    hideUnderline = typedArray.getBoolean(R.styleable.MaterialEditText_hideUnderline, false);
    typedArray.recycle();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        setBackground(null);
    } else {
        setBackgroundDrawable(null);
    }
    if (singleLineEllipsis) {
        TransformationMethod transformationMethod = getTransformationMethod();
        setSingleLine();
        setTransformationMethod(transformationMethod);
    }
    textPaint.setTextSize(floatingLabelTextSize);
    fontMetrics = textPaint.getFontMetrics();
    initMinBottomLines();
    initPadding();
    initText();
    initFloatingLabel();
    initErrorTextListener();
}
 
Example #20
Source File: CardEditTextTest.java    From android-card-form with MIT License 4 votes vote down vote up
@Test
public void remasksWhenFocusMovesAwayFromCardEditText() {
    TransformationMethod originalMethod = mView.getTransformationMethod();

    mView.setMask(true);

    type("4111111111111111");
    mView.onFocusChanged(false, 1, null);

    assertTrue(mView.getTransformationMethod() instanceof CardNumberTransformation);

    mView.onFocusChanged(true, 1, null);

    assertEquals(originalMethod, mView.getTransformationMethod());

    mView.onFocusChanged(false, 1, null);

    assertTrue(mView.getTransformationMethod() instanceof CardNumberTransformation);
}
 
Example #21
Source File: AutofitHelper.java    From kAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example #22
Source File: DSL.java    From anvil with MIT License 4 votes vote down vote up
public static Void transformationMethod(TransformationMethod arg) {
  return BaseDSL.attr("transformationMethod", arg);
}
 
Example #23
Source File: DSL.java    From anvil with MIT License 4 votes vote down vote up
public static Void transformationMethod(TransformationMethod arg) {
  return BaseDSL.attr("transformationMethod", arg);
}
 
Example #24
Source File: FireflyDialog.java    From smart-farmer-android with Apache License 2.0 4 votes vote down vote up
/**
 * 设置 EditText 的 transformationMethod
 */
public InputDialogBuilder setTransformationMethod(TransformationMethod transformationMethod) {
    mTransformationMethod = transformationMethod;
    return this;
}
 
Example #25
Source File: AutofitHelper.java    From DarkCalculator with MIT License 4 votes vote down vote up
/**
 * 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);
}
 
Example #26
Source File: AutofitHelper.java    From stynico with MIT License 4 votes vote down vote up
/**
    * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
    */
   private static void autofit(AppCompatTextView 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);
   }
 
Example #27
Source File: EditTextUtils.java    From DevUtils with Apache License 2.0 3 votes vote down vote up
/**
 * 设置文本视图显示转换
 * @param editText {@link EditText}
 * @param method   {@link TransformationMethod}
 * @param <T>      泛型
 * @return {@link EditText}
 */
public static <T extends EditText> T setTransformationMethod(final T editText, final TransformationMethod method) {
    if (editText != null) {
        editText.setTransformationMethod(method);
    }
    return editText;
}
 
Example #28
Source File: CardEditTextTest.java    From android-card-form with MIT License 3 votes vote down vote up
@Test
public void unmasksWhenFocusChangesBackToCardEditText() {
    TransformationMethod originalMethod = mView.getTransformationMethod();

    mView.setMask(true);

    type("4111111111111111");
    mView.onFocusChanged(false, 1, null);

    assertTrue(mView.getTransformationMethod() instanceof CardNumberTransformation);

    mView.onFocusChanged(true, 1, null);

    assertEquals(originalMethod, mView.getTransformationMethod());
}
 
Example #29
Source File: TextViewUtils.java    From DevUtils with Apache License 2.0 3 votes vote down vote up
/**
 * 设置文本视图显示转换
 * @param textView {@link TextView}
 * @param method   {@link TransformationMethod}
 * @param <T>      泛型
 * @return {@link TextView}
 */
public static <T extends TextView> T setTransformationMethod(final T textView, final TransformationMethod method) {
    if (textView != null) {
        textView.setTransformationMethod(method);
    }
    return textView;
}
 
Example #30
Source File: EditText.java    From MDPreference with Apache License 2.0 2 votes vote down vote up
/**
    * Sets the transformation that is applied to the text that this
    * TextView is displaying.
    *
    * @attr ref android.R.styleable#TextView_password
    * @attr ref android.R.styleable#TextView_singleLine
    */
public final void setTransformationMethod (TransformationMethod method){
	mInputView.setTransformationMethod(method);
}