Created
January 5, 2016 17:34
-
-
Save varren/4c5bced96d12c1231ac1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="horizontal" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin"> | |
<com.example.CustomView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="\uf1e2" | |
android:textSize="60dp" /> | |
<com.example.TextViewWithFont | |
xmlns:app="http://schemas.android.com/apk/res/com.example" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="\uf1e2" | |
android:textSize="60dp" | |
app:customFont="fontawesome-webfont.ttf" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="TextViewPlusFont"> | |
<attr name="customFont" format="string"/> | |
</declare-styleable> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.TextView; | |
public class CustomView extends TextView{ | |
public CustomView(Context context) { | |
this(context, null, 0); | |
} | |
public CustomView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
setTypeface(Typefaces.get(context, "fonts/fontawesome-webfont.ttf")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.util.AttributeSet; | |
import android.widget.TextView; | |
public class TextViewWithFont extends TextView { | |
public TextViewWithFont(Context context) { | |
super(context); | |
init(context, null, 0); | |
} | |
public TextViewWithFont(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs, 0); | |
} | |
public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(context, attrs, defStyle); | |
} | |
private void init(Context context, AttributeSet attrs, int defStyle) { | |
// Load attributes | |
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextViewPlusFont, 0, 0); | |
try { | |
String fontInAssets = ta.getString(R.styleable.TextViewPlusFont_customFont); | |
setTypeface(Typefaces.get(context, "fonts/"+ fontInAssets)); | |
} finally { | |
ta.recycle(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.vestl_000.stackoverflow_voley; | |
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.util.Log; | |
import java.util.Hashtable; | |
public class Typefaces { | |
private static final String TAG = "Typefaces"; | |
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>(); | |
public static Typeface get(Context c, String assetPath) { | |
synchronized (cache) { | |
if (!cache.containsKey(assetPath)) { | |
try { | |
Typeface t = Typeface.createFromAsset(c.getAssets(), | |
assetPath); | |
cache.put(assetPath, t); | |
Log.e(TAG, "Loaded '" + assetPath); | |
} catch (Exception e) { | |
Log.e(TAG, "Could not get typeface '" + assetPath | |
+ "' because " + e.getMessage()); | |
return null; | |
} | |
} | |
return cache.get(assetPath); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment