Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. Textview tv = new TextView(this);
  2. tv.setTextSize(20);
  3.  
  4. <TextView android:text="@+id/TextView01"
  5. android:id="@+id/TextView01"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:textSize="30px"
  9. android:textStyle="italic"
  10. android:typeface="serif" />
  11.  
  12. TextView tv=(TextView)findViewById(R.id.custom);
  13. Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
  14. tv.setTypeface(face);
  15.  
  16. TextView text = new TextView(this);
  17. text.setTypeface(Typeface.createFromAsset(getAssets(), "default.ttf"));
  18.  
  19. text.setTextSize(20);
  20.  
  21. public class TypefaceHelper {
  22.  
  23. public static void setViewGroupTypeface(ViewGroup container, Typeface typeface) {
  24. final int children = container.getChildCount();
  25.  
  26. for (int i = 0; i < children; i++)
  27. View child = container.getChildAt(i);
  28.  
  29. if (child instanceof TextView) {
  30. setTextViewTypeface((TextView) child, typeface);
  31. } else if (child instanceof ViewGroup) {
  32. setViewGroupTypeface((ViewGroup) child, typeface);
  33. }
  34. }
  35. }
  36.  
  37. public static void setTextViewTypeface(TextView textView, Typeface typeface) {
  38. textView.setTypeface(typeface);
  39. }
  40.  
  41. }
  42.  
  43. For Custom font:
  44.  
  45. TextView tv = ((TextView) v.findViewById(R.id.select_item_title));
  46. Typeface face=Typeface.createFromAsset(getAssets(),"fonts/mycustomfont.ttf");
  47. tv.setTypeface(face);
  48.  
  49. For Default font:
  50.  
  51. tv.setTypeface(Typeface.create("sans-serif-medium",Typeface.NORMAL));
  52.  
  53. FONT FAMILY TTF FILE
  54.  
  55. 1 casual ComingSoon.ttf
  56. 2 cursive DancingScript-Regular.ttf
  57. 3 monospace DroidSansMono.ttf
  58. 4 sans-serif Roboto-Regular.ttf
  59. 5 sans-serif-black Roboto-Black.ttf
  60. 6 sans-serif-condensed RobotoCondensed-Regular.ttf
  61. 7 sans-serif-condensed-light RobotoCondensed-Light.ttf
  62. 8 sans-serif-light Roboto-Light.ttf
  63. 9 sans-serif-medium Roboto-Medium.ttf
  64. 10 sans-serif-smallcaps CarroisGothicSC-Regular.ttf
  65. 11 sans-serif-thin Roboto-Thin.ttf
  66. 12 serif NotoSerif-Regular.ttf
  67. 13 serif-monospace CutiveMono.ttf
  68.  
  69. private static final String FONT_NAME = "fonts/Roboto-Regular.ttf";
  70. private static Typeface m_font = null;
  71.  
  72. public static Typeface getFont(Context p_context)
  73. {
  74. if (null == m_font && null != p_context)
  75. {
  76. m_font = Typeface.createFromAsset(p_context.getApplicationContext().getAssets(), FONT_NAME);
  77. }
  78. return m_font;
  79. }
  80.  
  81. public static void setViewGroupFont(ViewGroup p_viewGroup)
  82. {
  83. if (null != p_viewGroup)
  84. {
  85. for (int currChildIndex = 0; currChildIndex < p_viewGroup.getChildCount(); currChildIndex++)
  86. {
  87. View currChildView = p_viewGroup.getChildAt(currChildIndex);
  88.  
  89. if (ViewGroup.class.isInstance(currChildView))
  90. {
  91. setViewGroupFont((ViewGroup) currChildView);
  92. }
  93. else
  94. {
  95. try
  96. {
  97. Method setTypefaceMethod = currChildView.getClass().getMethod("setTypeface", Typeface.class);
  98.  
  99. setTypefaceMethod.invoke(currChildView, getFont(p_viewGroup.getContext()));
  100. }
  101. catch (NoSuchMethodException ex)
  102. {
  103. // Do nothing
  104. }
  105. catch (Exception ex)
  106. {
  107. // Unexpected error setting font
  108. }
  109. }
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement