Example usage for android.widget TextView getTypeface

List of usage examples for android.widget TextView getTypeface

Introduction

In this page you can find the example usage for android.widget TextView getTypeface.

Prototype

public Typeface getTypeface() 

Source Link

Document

Gets the current Typeface that is used to style the text.

Usage

From source file:Main.java

public static String getDefaultFontWeight(Context context) {
    String style = "normal";
    TextView tv = new TextView(context);
    if (tv != null) {
        Typeface tf = tv.getTypeface();
        if (tf != null && tf.isBold()) {
            style = "bold";
        }//from  ww w.  j a v a  2s  .c  o m
    }

    return style;
}

From source file:org.mariotaku.twidere.util.ThemedLayoutInflaterFactory.java

public static void initView(View view, IThemedActivity activity) {
    if (view == null)
        return;/* w ww  .j a  v  a2s.co  m*/
    if (view instanceof ShapedImageView) {
        final ShapedImageView shapedImageView = (ShapedImageView) view;
        shapedImageView.setStyle(activity.getCurrentProfileImageStyle());
    }
    if (view instanceof TextView && (!(view instanceof ICustomTypefaceTextView))) {
        final String fontFamily = activity.getCurrentThemeFontFamily();
        final TextView textView = (TextView) view;
        final Typeface defTypeface = textView.getTypeface();
        textView.setTypeface(ThemeUtils.getUserTypeface((Context) activity, fontFamily, defTypeface));
    }
    initViewTint(view, activity);
}

From source file:android.support.v17.leanback.app.ErrorSupportFragment.java

private static FontMetricsInt getFontMetricsInt(TextView textView) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textView.getTextSize());
    paint.setTypeface(textView.getTypeface());
    return paint.getFontMetricsInt();
}

From source file:com.facebook.appevents.codeless.internal.ViewHierarchy.java

public static JSONObject setAppearanceOfView(View view, JSONObject json, float displayDensity) {
    try {//from w w  w  .j  av a2 s.  com
        JSONObject textStyle = new JSONObject();
        if (view instanceof TextView) {
            TextView textView = (TextView) view;
            Typeface typeface = textView.getTypeface();
            if (typeface != null) {
                textStyle.put(TEXT_SIZE, textView.getTextSize());
                textStyle.put(TEXT_IS_BOLD, typeface.isBold());
                textStyle.put(TEXT_IS_ITALIC, typeface.isItalic());
                json.put(TEXT_STYLE, textStyle);
            }
        }
        if (view instanceof ImageView) {
            Drawable drawable = ((ImageView) view).getDrawable();
            if (drawable instanceof BitmapDrawable) {
                if (view.getHeight() / displayDensity <= ICON_MAX_EDGE_LENGTH
                        && view.getWidth() / displayDensity <= ICON_MAX_EDGE_LENGTH) {
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    byte[] byteArray = byteArrayOutputStream.toByteArray();
                    String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
                    json.put(ICON_BITMAP, encoded);
                }
            }
        }
    } catch (JSONException e) {
        Utility.logd(TAG, e);
    }

    return json;
}

From source file:com.fastbootmobile.encore.utils.Utils.java

/**
 * Calculates the optimal size of the text based on the text view width
 *
 * @param textView     The text view in which the text should fit
 * @param desiredWidth The desired final text width, or -1 for the TextView's getMeasuredWidth
 *//*from www.java 2 s  . c om*/
public static void adjustTextSize(TextView textView, int desiredWidth) {
    if (desiredWidth <= 0) {
        desiredWidth = textView.getMeasuredWidth();

        if (desiredWidth <= 0) {
            // Invalid width, don't do anything
            Log.w("Utils", "adjustTextSize: Not doing anything (measured width invalid)");
            return;
        }
    }

    // Add some margin to width
    desiredWidth *= 0.8f;

    Paint paint = new Paint();
    Rect bounds = new Rect();

    paint.setTypeface(textView.getTypeface());
    float textSize = textView.getTextSize() * 2.0f;
    paint.setTextSize(textSize);
    String text = textView.getText().toString();
    paint.getTextBounds(text, 0, text.length(), bounds);

    while (bounds.width() > desiredWidth) {
        textSize--;
        paint.setTextSize(textSize);
        paint.getTextBounds(text, 0, text.length(), bounds);
    }

    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}

From source file:de.vanita5.twittnuker.util.ThemeUtils.java

public static void initTextView(TextView view) {
    if (view.isInEditMode())
        return;//from   w  ww  .  j  ava 2  s.  c  om
    final Context context = view.getContext();
    //        view.setLinkTextColor(ThemeUtils.getUserLinkTextColor(context));
    //        view.setHighlightColor(ThemeUtils.getUserHighlightColor(context));
    view.setTypeface(ThemeUtils.getUserTypeface(context, view.getTypeface()));
}

From source file:com.android.deskclock.timer.TimerView.java

/**
 * Measure the text and add a start padding to the view
 * @param textView view to measure and onb to which add start padding
 *//*from w  w w  .  j  a  va  2 s  .c  o m*/
private void addStartPadding(TextView textView) {
    final float gapPadding = 0.45f;
    // allDigits will contain ten digits: "0123456789" in the default locale
    String allDigits = String.format(Locale.getDefault(), "%010d", 123456789);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textView.getTextSize());
    paint.setTypeface(textView.getTypeface());

    float widths[] = new float[allDigits.length()];
    int ll = paint.getTextWidths(allDigits, widths);
    int largest = 0;
    for (int ii = 1; ii < ll; ii++) {
        if (widths[ii] > widths[largest]) {
            largest = ii;
        }
    }
    // Add left padding to the view - Note: layout inherits LTR
    textView.setPadding((int) (gapPadding * widths[largest]), 0, 0, 0);
}

From source file:me.henrytao.mdcore.core.MdLayoutInflaterFactory.java

protected void supportTypeface(Context context, TextView textView, AttributeSet attrs) {
    if (textView == null) {
        return;/*from  w  ww  . j  av  a 2  s.  c o m*/
    }
    Typeface typeface = Typography.getTypeface(context, attrs,
            textView instanceof Button ? R.attr.buttonStyle : 0, 0);
    typeface = typeface != null ? typeface : Typography.getDefaultTypeface(context);
    if (typeface != null && textView.getTypeface() != typeface) {
        textView.setTypeface(typeface);
    }
}

From source file:larry.baby.rain.common.view.TabPageIndicator.java

private void addTab(CharSequence text, int index) {
    Context context = getContext();
    DisplayMetrics dm = context.getResources().getDisplayMetrics();

    int padX = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 22, dm);
    int padY = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, dm);

    TextView tabView = new TextView(context);
    tabView.setBackgroundResource(R.drawable.vpi__tab_indicator);
    tabView.setPadding(padX, padY, padX, padY);
    tabView.setGravity(Gravity.CENTER);/* w  w  w .  jav  a 2  s  . co  m*/
    tabView.setTextColor(0xfff3f3f3);
    tabView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
    tabView.setTypeface(tabView.getTypeface(), Typeface.BOLD);
    tabView.setSingleLine();
    tabView.setTag(index);
    tabView.setFocusable(true);
    tabView.setOnClickListener(this);
    tabView.setText(text);

    mTabLayout.addView(tabView, new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1));
}

From source file:com.lloydtorres.stately.helpers.SparkleHelper.java

/**
 * Helper used for setting and styling an HTML string into a TextView.
 * @param c App context/*from   w  w  w .j  av  a 2  s . com*/
 * @param t Target TextView
 * @param holder Content
 */
public static void setStyledTextView(Context c, TextView t, String holder) {
    if (t instanceof HtmlTextView) {
        try {
            ((HtmlTextView) t).setHtml(holder);
        } catch (Exception e) {
            logError(e.toString());
            t.setText(c.getString(R.string.bbcode_parse_error));
            t.setTypeface(t.getTypeface(), Typeface.ITALIC);
        }
    } else {
        t.setText(fromHtml(holder));
    }
    styleLinkifiedTextView(c, t);
}