Created
November 30, 2017 13:00
-
-
Save rliuzzi/907f12f93aab99ced8ccdff77ab8e98e to your computer and use it in GitHub Desktop.
Custom Text View with Roboto font cheatsheet
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="CustomTextView"> | |
<attr name="customTextViewDrawableTint" format="color"/> | |
<attr name="customTextViewDrawableLeftCompat" format="reference"/> | |
<attr name="customTextViewDrawableRightCompat" format="reference"/> | |
<attr name="customTextViewDrawableTopCompat" format="reference"/> | |
<attr name="customTextViewDrawableBottomCompat" format="reference"/> | |
<attr name="customTextViewFont" 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
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Color; | |
import android.graphics.PorterDuff; | |
import android.graphics.drawable.Drawable; | |
import android.os.Build; | |
import android.support.v4.graphics.drawable.DrawableCompat; | |
import android.support.v7.content.res.AppCompatResources; | |
import android.support.v7.widget.AppCompatTextView; | |
import android.util.AttributeSet; | |
public class CustomTextView extends AppCompatTextView { | |
int tintColor; | |
Drawable drawableLeft = null; | |
Drawable drawableRight = null; | |
Drawable drawableBottom = null; | |
Drawable drawableTop = null; | |
String fontTypeface = null; | |
public CustomTextView(Context context) { | |
super(context); | |
} | |
public CustomTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
private void init(Context context, AttributeSet attrs) { | |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IcabbiTextView, 0, 0); | |
fontTypeface = a.getString(R.styleable.CustomTextView_customTextViewFont); | |
tintColor = a.getColor(R.styleable.CustomTextView_customTextViewDrawableTint, Color.BLACK); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
drawableLeft = a.getDrawable(R.styleable.CustomTextView_customTextViewDrawableLeftCompat); | |
drawableRight = a.getDrawable(R.styleable.CustomTextView_customTextViewDrawableRightCompat); | |
drawableBottom = a.getDrawable(R.styleable.CustomTextView_customTextViewDrawableBottomCompat); | |
drawableTop = a.getDrawable(R.styleable.CustomTextView_customTextViewDrawableTopCompat); | |
} else { | |
final int drawableLeftId = a.getResourceId(R.styleable.CustomTextView_customTextViewDrawableLeftCompat, -1); | |
final int drawableRightId = a.getResourceId(R.styleable.CustomTextView_customTextViewDrawableRightCompat, -1); | |
final int drawableBottomId = a.getResourceId(R.styleable.CustomTextView_customTextViewDrawableBottomCompat, -1); | |
final int drawableTopId = a.getResourceId(R.styleable.CustomTextView_customTextViewDrawableTopCompat, -1); | |
if (drawableLeftId != -1) | |
drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId); | |
if (drawableRightId != -1) | |
drawableRight = AppCompatResources.getDrawable(context, drawableRightId); | |
if (drawableBottomId != -1) | |
drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId); | |
if (drawableTopId != -1) | |
drawableTop = AppCompatResources.getDrawable(context, drawableTopId); | |
} | |
a.recycle(); | |
if (!isInEditMode()) { | |
RobotoTypeface font = null; | |
if(fontTypeface != null) { | |
font = RobotoTypeface.valueOf(fontTypeface); | |
} | |
setTypeface(FontLoader.getInstance(getContext()).getFont(font)); | |
} | |
drawableLeft = setTintForDrawable(drawableLeft); | |
drawableRight = setTintForDrawable(drawableRight); | |
drawableTop = setTintForDrawable(drawableTop); | |
drawableBottom = setTintForDrawable(drawableBottom); | |
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom); | |
} | |
private Drawable setTintForDrawable(Drawable drawable){ | |
if (drawable!=null) { | |
drawable = drawable.mutate(); | |
drawable = DrawableCompat.wrap(drawable); | |
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_ATOP); | |
DrawableCompat.setTint(drawable, tintColor); | |
} | |
return drawable; | |
} | |
public void setTintColor(int tintColor) { | |
this.tintColor = tintColor; | |
} | |
public void setDrawableLeft(Drawable drawable) { | |
this.drawableLeft = setTintForDrawable(drawable); | |
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom); | |
} | |
public void setDrawableRight(Drawable drawable) { | |
this.drawableRight = setTintForDrawable(drawable); | |
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom); | |
} | |
public void setDrawableBottom(Drawable drawable) { | |
this.drawableBottom = setTintForDrawable(drawable); | |
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom); | |
} | |
public void setDrawableTop(Drawable drawable) { | |
this.drawableTop = setTintForDrawable(drawable); | |
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom); | |
} | |
} |
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" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<CustomTextView | |
android:id="@+id/splash_name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
tools:text="Nice Roboto font" | |
app:customTextViewFont="Light" | |
android:textScaleX="1.15" /> | |
</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
public class FontLoader { | |
private Context context; | |
private static FontLoader instance; | |
public static FontLoader getInstance(Context context) { | |
if (instance == null) { | |
instance = new FontLoader(context); | |
} | |
return instance; | |
} | |
private FontLoader(Context context) { | |
if (context!=null) { | |
this.context = context.getApplicationContext(); | |
} | |
} | |
public Typeface getFont(RobotoTypeface typeface){ | |
String assetFile = RobotoTypeface.getResourceName(RobotoTypeface.Regular); | |
if(typeface != null){ | |
assetFile = typeface.getResourceName(typeface); | |
} | |
return Typeface.createFromAsset(context.getAssets(), assetFile); | |
} | |
} |
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
public enum RobotoTypeface { | |
Bold, | |
Light, | |
Medium, | |
Regular; | |
// Download Font: https://fonts.google.com/specimen/Roboto?selection.family=Roboto | |
// Add to project assets/Roboto-Bold.ttf, assets/Roboto-Light.ttf, ... | |
public static String getResourceName(RobotoTypeface typeface){ | |
return "Roboto-".concat(typeface.name()).concat(".ttf"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment