Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Textview tv = new TextView(this);
- tv.setTextSize(20);
- <TextView android:text="@+id/TextView01"
- android:id="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="30px"
- android:textStyle="italic"
- android:typeface="serif" />
- TextView tv=(TextView)findViewById(R.id.custom);
- Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
- tv.setTypeface(face);
- TextView text = new TextView(this);
- text.setTypeface(Typeface.createFromAsset(getAssets(), "default.ttf"));
- text.setTextSize(20);
- public class TypefaceHelper {
- public static void setViewGroupTypeface(ViewGroup container, Typeface typeface) {
- final int children = container.getChildCount();
- for (int i = 0; i < children; i++)
- View child = container.getChildAt(i);
- if (child instanceof TextView) {
- setTextViewTypeface((TextView) child, typeface);
- } else if (child instanceof ViewGroup) {
- setViewGroupTypeface((ViewGroup) child, typeface);
- }
- }
- }
- public static void setTextViewTypeface(TextView textView, Typeface typeface) {
- textView.setTypeface(typeface);
- }
- }
- For Custom font:
- TextView tv = ((TextView) v.findViewById(R.id.select_item_title));
- Typeface face=Typeface.createFromAsset(getAssets(),"fonts/mycustomfont.ttf");
- tv.setTypeface(face);
- For Default font:
- tv.setTypeface(Typeface.create("sans-serif-medium",Typeface.NORMAL));
- FONT FAMILY TTF FILE
- 1 casual ComingSoon.ttf
- 2 cursive DancingScript-Regular.ttf
- 3 monospace DroidSansMono.ttf
- 4 sans-serif Roboto-Regular.ttf
- 5 sans-serif-black Roboto-Black.ttf
- 6 sans-serif-condensed RobotoCondensed-Regular.ttf
- 7 sans-serif-condensed-light RobotoCondensed-Light.ttf
- 8 sans-serif-light Roboto-Light.ttf
- 9 sans-serif-medium Roboto-Medium.ttf
- 10 sans-serif-smallcaps CarroisGothicSC-Regular.ttf
- 11 sans-serif-thin Roboto-Thin.ttf
- 12 serif NotoSerif-Regular.ttf
- 13 serif-monospace CutiveMono.ttf
- private static final String FONT_NAME = "fonts/Roboto-Regular.ttf";
- private static Typeface m_font = null;
- public static Typeface getFont(Context p_context)
- {
- if (null == m_font && null != p_context)
- {
- m_font = Typeface.createFromAsset(p_context.getApplicationContext().getAssets(), FONT_NAME);
- }
- return m_font;
- }
- public static void setViewGroupFont(ViewGroup p_viewGroup)
- {
- if (null != p_viewGroup)
- {
- for (int currChildIndex = 0; currChildIndex < p_viewGroup.getChildCount(); currChildIndex++)
- {
- View currChildView = p_viewGroup.getChildAt(currChildIndex);
- if (ViewGroup.class.isInstance(currChildView))
- {
- setViewGroupFont((ViewGroup) currChildView);
- }
- else
- {
- try
- {
- Method setTypefaceMethod = currChildView.getClass().getMethod("setTypeface", Typeface.class);
- setTypefaceMethod.invoke(currChildView, getFont(p_viewGroup.getContext()));
- }
- catch (NoSuchMethodException ex)
- {
- // Do nothing
- }
- catch (Exception ex)
- {
- // Unexpected error setting font
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement