Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.83 KB | None | 0 0
  1. public class ButtonPlus extends Button {
  2.  
  3. public ButtonPlus(Context context) {
  4. super(context);
  5. }
  6.  
  7. public ButtonPlus(Context context, AttributeSet attrs) {
  8. super(context, attrs);
  9. CustomFontHelper.setCustomFont(this, context, attrs);
  10. }
  11.  
  12. public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
  13. super(context, attrs, defStyle);
  14. CustomFontHelper.setCustomFont(this, context, attrs);
  15. }
  16. }
  17.  
  18. public class CustomFontHelper {
  19.  
  20. /**
  21. * Sets a font on a textview based on the custom com.my.package:font attribute
  22. * If the custom font attribute isn't found in the attributes nothing happens
  23. * @param textview
  24. * @param context
  25. * @param attrs
  26. */
  27. public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) {
  28. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
  29. String font = a.getString(R.styleable.CustomFont_font);
  30. setCustomFont(textview, font, context);
  31. a.recycle();
  32. }
  33.  
  34. /**
  35. * Sets a font on a textview
  36. * @param textview
  37. * @param font
  38. * @param context
  39. */
  40. public static void setCustomFont(TextView textview, String font, Context context) {
  41. if(font == null) {
  42. return;
  43. }
  44. Typeface tf = FontCache.get(font, context);
  45. if(tf != null) {
  46. textview.setTypeface(tf);
  47. }
  48. }
  49.  
  50. }
  51.  
  52. public class FontCache {
  53.  
  54. private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();
  55.  
  56. public static Typeface get(String name, Context context) {
  57. Typeface tf = fontCache.get(name);
  58. if(tf == null) {
  59. try {
  60. tf = Typeface.createFromAsset(context.getAssets(), name);
  61. }
  62. catch (Exception e) {
  63. return null;
  64. }
  65. fontCache.put(name, tf);
  66. }
  67. return tf;
  68. }
  69. }
  70.  
  71. <?xml version="1.0" encoding="utf-8"?>
  72. <resources>
  73. <declare-styleable name="CustomFont">
  74. <attr name="font" format="string"/>
  75. </declare-styleable>
  76. </resources>
  77.  
  78. <com.my.package.buttons.ButtonPlus
  79. style="@style/button"
  80. android:layout_width="wrap_content"
  81. android:layout_height="wrap_content"
  82. android:text="@string/button_sometext"/>
  83.  
  84. <style name="button" parent="@android:style/Widget.Button">
  85. <item name="com.my.package:font">fonts/copperplate_gothic_light.TTF</item>
  86. </style>
  87.  
  88. Typeface copperplateGothicLight = Typeface.createFromAsset(getAppContext().getAssets(), "CopperplateGothicLight.ttf");
  89. yourButton.setTypeface(copperplateGothicLight);
  90.  
  91. public class CustomButton extends Button {
  92.  
  93. Typeface normalTypeface = FontCache.get("fonts/CopperplateGothicLight.ttf", getContext());
  94. Typeface boldTypeface = FontCache.get("fonts/CopperplateGothicBold.ttf", getContext());
  95.  
  96. /**
  97. * @param context
  98. */
  99. public CustomButton(Context context) {
  100. super(context);
  101. }
  102.  
  103. /**
  104. * @param context
  105. * @param attrs
  106. */
  107. public CustomButton(Context context, AttributeSet attrs) {
  108. super(context, attrs);
  109. }
  110.  
  111. /**
  112. * @param context
  113. * @param attrs
  114. * @param defStyleAttr
  115. */
  116. public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
  117. super(context, attrs, defStyleAttr);
  118. }
  119.  
  120. }
  121.  
  122. public class FontCache {
  123. private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();
  124.  
  125. public static Typeface get(String name, Context context) {
  126. Typeface tf = fontCache.get(name);
  127. if(tf == null) {
  128. try {
  129. tf = Typeface.createFromAsset(context.getAssets(), name);
  130. }
  131. catch (Exception e) {
  132. return null;
  133. }
  134. fontCache.put(name, tf);
  135. }
  136. return tf;
  137. }
  138. }
  139.  
  140. package com.mehuljoisar.customfontdemo;
  141.  
  142. import android.app.Activity;
  143. import android.graphics.Typeface;
  144. import android.os.Bundle;
  145. import android.view.Menu;
  146. import android.widget.Button;
  147.  
  148. public class MainActivity extends Activity {
  149.  
  150. private Button button1;
  151.  
  152. @Override
  153. protected void onCreate(Bundle savedInstanceState) {
  154. super.onCreate(savedInstanceState);
  155. setContentView(R.layout.activity_main);
  156.  
  157. button1 = (Button)findViewById(R.id.button1);
  158. button1.setTypeface(Typeface.createFromAsset(getAssets(), "copperplate-gothic-light.ttf"));
  159. button1.setText("hello");
  160. }
  161.  
  162. @Override
  163. public boolean onCreateOptionsMenu(Menu menu) {
  164. // Inflate the menu; this adds items to the action bar if it is present.
  165. getMenuInflater().inflate(R.menu.activity_main, menu);
  166. return true;
  167. }
  168.  
  169. }
  170.  
  171. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  172. xmlns:tools="http://schemas.android.com/tools"
  173. android:layout_width="match_parent"
  174. android:layout_height="match_parent"
  175. tools:context=".MainActivity" >
  176.  
  177. <TextView
  178. android:layout_width="wrap_content"
  179. android:layout_height="wrap_content"
  180. android:layout_centerHorizontal="true"
  181. android:layout_centerVertical="true"
  182. android:text="@string/hello_world" />
  183.  
  184. <Button
  185. android:id="@+id/button1"
  186. android:layout_width="wrap_content"
  187. android:layout_height="wrap_content"
  188. android:layout_alignParentLeft="true"
  189. android:layout_alignParentTop="true"
  190. android:layout_marginTop="24dp"
  191. android:text="Button" />
  192.  
  193. Typeface font_style = Typeface.createFromAsset(getAssets(), "yourcystomfontstyle.ttf");
  194. yourbutton.setTypeface(font_style);
  195.  
  196. public class CustomButton extends Button{
  197.  
  198.  
  199. public CustomButton(Context context, AttributeSet attrs) {
  200. super(context, attrs);
  201. init();
  202. // TODO Auto-generated constructor stub
  203. }
  204. public CustomButton(Context context) {
  205. super(context);
  206. init();
  207. // TODO Auto-generated constructor stub
  208. }
  209. public CustomButton(Context context, AttributeSet attrs, int defStyle) {
  210. super(context, attrs, defStyle);
  211. init();
  212. // TODO Auto-generated constructor stub
  213. }
  214. private void init(){
  215. Typeface font_type=Typeface.createFromAsset(getContext().getAssets(), "font/ProximaNova-Bold.ttf");
  216. setTypeface(font_type);
  217. }
  218. }
  219.  
  220. <model.CustomButton
  221. android:id="@+id/search"
  222. android:layout_width="@dimen/edittext_width_large"
  223. android:layout_height="@dimen/button_height"
  224. android:layout_below="@+id/cars"
  225. android:layout_centerHorizontal="true"
  226. android:layout_marginTop="@dimen/pad_20dp"
  227. android:background="@drawable/button_pressed_bg"
  228. android:text="@string/find_car"
  229. android:textColor="@color/white" />
  230.  
  231. <your.namespace.app.FontButton
  232. app:font="montserrat"
  233. android:layout_width="wrap_content"
  234. android:layout_height="wrap_content"/>
  235.  
  236. public class FontButton extends Button {
  237.  
  238. public FontEditText(Context context) {
  239. this( context, null );
  240. }
  241.  
  242. public FontEditText(Context context, AttributeSet attrs) {
  243. this( context, attrs, 0 );
  244. init( context, attrs );
  245. }
  246.  
  247. public FontEditText(Context context, AttributeSet attrs, int defStyle) {
  248. super( context, attrs, defStyle );
  249. init( context, attrs );
  250. }
  251.  
  252. public FontEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  253. super( context, attrs, defStyleAttr, defStyleRes );
  254. init( context, attrs );
  255. }
  256.  
  257. private void init(Context context, AttributeSet attrs) {
  258. TypedArray ta = context.obtainStyledAttributes( attrs, R.styleable.Fonts );
  259.  
  260. if ( ta != null ) {
  261. String fontAsset = ta.getString( R.styleable.Fonts_font );
  262. if ( !StringUtils.isEmpty( fontAsset ) ) {
  263. int type = Integer.parseInt( fontAsset );
  264.  
  265. Typeface typeFace = FontManager.getInstance( context ).getByType( type );
  266. ta.recycle();
  267. super.setTypeface( typeFace );
  268. }
  269. }
  270. }
  271.  
  272. }
  273.  
  274. public class FontManager {
  275.  
  276. private static FontManager Instance;
  277.  
  278. private Context context;
  279.  
  280. private Typeface robotoCondensedBold;
  281. private Typeface robotoCondensed;
  282. private Typeface robotoLight;
  283. private Typeface kronica;
  284. private Typeface montserrat;
  285. private Typeface montserratLight;
  286. private Typeface keepCalmMedium;
  287.  
  288. private FontManager(Context context) {
  289. this.context = context;
  290. this.robotoCondensedBold = Typeface.createFromAsset( context.getAssets(), "fonts/RobotoCondensed-Bold.ttf" );
  291. this.robotoCondensed = Typeface.createFromAsset( context.getAssets(), "fonts/RobotoCondensed-Regular.ttf" );
  292. this.robotoLight = Typeface.createFromAsset( context.getAssets(), "fonts/Roboto-Light.ttf" );
  293. this.kronica = Typeface.createFromAsset( context.getAssets(), "fonts/kronika.ttf" );
  294. this.montserrat = Typeface.createFromAsset( context.getAssets(), "fonts/Montserrat-Regular.ttf" );
  295. this.montserratLight = Typeface.createFromAsset( context.getAssets(), "fonts/Montserrat-Light.ttf" );
  296. this.keepCalmMedium = Typeface.createFromAsset( context.getAssets(), "fonts/KeepCalmMedium.ttf" );
  297. }
  298.  
  299. public synchronized static FontManager getInstance(Context context) {
  300. if ( Instance == null )
  301. Instance = new FontManager( context );
  302.  
  303. return Instance;
  304. }
  305.  
  306. public Typeface getByType(int type) {
  307. switch ( type ) {
  308. case 0:
  309. return FontManager.getInstance( context ).getRobotoCondensedBold();
  310. case 1:
  311. return FontManager.getInstance( context ).getRobotoLight();
  312. case 2:
  313. return FontManager.getInstance( context ).getKronica();
  314. case 3:
  315. return FontManager.getInstance( context ).getRobotoCondensed();
  316. case 4:
  317. return FontManager.getInstance( context ).getMontserrat();
  318. case 5:
  319. return FontManager.getInstance( context ).getMontserratLight();
  320. case 6:
  321. return FontManager.getInstance( context ).getKeepCalmMedium();
  322. default:
  323. return Typeface.DEFAULT;
  324. }
  325. }
  326.  
  327. public Typeface getRobotoCondensedBold() {
  328. return robotoCondensedBold;
  329. }
  330.  
  331. public Typeface getKronica() {
  332. return kronica;
  333. }
  334.  
  335. public Typeface getRobotoCondensed() {
  336. return robotoCondensed;
  337. }
  338.  
  339. public Typeface getRobotoLight() {
  340. return robotoLight;
  341. }
  342.  
  343. public Typeface getMontserrat() {
  344. return montserrat;
  345. }
  346.  
  347. public Typeface getMontserratLight() {
  348. return montserratLight;
  349. }
  350.  
  351. public Typeface getKeepCalmMedium() {
  352. return keepCalmMedium;
  353. }
  354.  
  355. <?xml version="1.0" encoding="utf-8"?>
  356. <resources>
  357. <declare-styleable name="Fonts">
  358. <attr name="font" format="enum">
  359. <enum name="robotoCondensedBold" value="0"/>
  360. <enum name="robotoLight" value="1"/>
  361. <enum name="kronica" value="2"/>
  362. <enum name="robotoCondensed" value="3"/>
  363. <enum name="montserrat" value="4"/>
  364. <enum name="montserratLight" value="5"/>
  365. <enum name="keepCalmMedium" value="6"/>
  366. </attr>
  367. </declare-styleable>
  368. </resources>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement