Skip to content

Instantly share code, notes, and snippets.

@JavierSolis
Created March 1, 2018 17:12
Show Gist options
  • Save JavierSolis/baad49d7ab0ca2cb0cc4093052b93079 to your computer and use it in GitHub Desktop.
Save JavierSolis/baad49d7ab0ca2cb0cc4093052b93079 to your computer and use it in GitHub Desktop.
Clase utilitaria para setear fuente a las vistas
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
/**
* by Javier Solis @JavierTwiteando
* Github http://bit.ly/onGithub
*/
public class UtilFont {
public static String ARIAL = "arial.ttf";
public static String SANTOR = "santor.otf";
public static String HELVETICA_BOLD = "helvetica_bold.ttf";
public static String ESPHIMERE = "esphimere.otf";
public static String ESPHIMERE_BOLD = "esphimere_bold.otf";
public Context context;
public UtilFont(Context context) {
this.context = context;
}
public static void setFontArial(Activity activity, int id) {
TextView textView = (TextView) activity.findViewById(id);
Typeface typeFace = Typeface.createFromAsset(activity.getAssets(), "arial.ttf");
textView.setTypeface(typeFace);
}
public static void setFontSantor(Activity activity, int id) {
TextView textView = (TextView) activity.findViewById(id);
Typeface typeFace = Typeface.createFromAsset(activity.getAssets(), "santor.otf");
textView.setTypeface(typeFace);
}
public static void setFontSantorEt(Activity activity, int id) {
EditText textView = (EditText) activity.findViewById(id);
Typeface typeFace = Typeface.createFromAsset(activity.getAssets(), "santor.otf");
textView.setTypeface(typeFace);
}
public static void setFont(Activity activity, int id, String fontName) throws Exception {
View view = activity.findViewById(id);
Typeface typeFace = Typeface.createFromAsset(activity.getAssets(), fontName);
if (view instanceof TextView) {
// Set the font if it is a TextView.
((TextView) view).setTypeface(typeFace);
return;
} else if (view instanceof EditText) {
// Set the font if it is a TextView.
((EditText) view).setTypeface(typeFace);
return;
} else {
throw new Exception("no tiene donde setear");
}
}
public static void setFontFrag(View v, Activity activity, int id, String fontName) throws Exception {
View view = v.findViewById(id);
Typeface typeFace = Typeface.createFromAsset(activity.getAssets(), fontName);
if (view instanceof TextView) {
// Set the font if it is a TextView.
((TextView) view).setTypeface(typeFace);
return;
} else if (view instanceof EditText) {
// Set the font if it is a TextView.
((EditText) view).setTypeface(typeFace);
return;
} else {
throw new Exception("no tiene donde setear");
}
}
public static void setFontGroup(Activity activity, int idChilGroup, String fontName) {
final ViewGroup mContainer = (ViewGroup) activity.findViewById(idChilGroup).getRootView();
final Typeface mFont = Typeface.createFromAsset(activity.getAssets(), fontName);
setAppFont(mContainer, mFont);
}
public static void setFontGroupByRoot(Activity activity, int idGroup, String fontName) {
final ViewGroup mContainer = (ViewGroup) activity.findViewById(idGroup).getRootView();
final Typeface mFont = Typeface.createFromAsset(activity.getAssets(), fontName);
setAppFont(mContainer, mFont);
}
public static void setFontAllFrag(View v,Activity activity, String fontName) {
final ViewGroup mContainer = (ViewGroup) v.getRootView();
final Typeface mFont = Typeface.createFromAsset(activity.getAssets(), fontName);
setAppFont(mContainer, mFont);
}
public static void setFontGroupByRootFragment(View v,Activity activity, int idGroup, String fontName) {
final ViewGroup mContainer = (ViewGroup) v.findViewById(idGroup);
final Typeface mFont = Typeface.createFromAsset(activity.getAssets(), fontName);
setAppFont(mContainer, mFont);
}
public static void setFontAllActivity(Activity activity, String fontName){
final ViewGroup mContainer = (ViewGroup) activity.findViewById(android.R.id.content).getRootView();
final Typeface mFont = Typeface.createFromAsset(activity.getAssets(), fontName);
setAppFont(mContainer, mFont);
}
public static final void setAppFont(ViewGroup mContainer, Typeface mFont) {
if (mContainer == null || mFont == null)
return;
final int mCount = mContainer.getChildCount();
// Loop through all of the children.
for (int i = 0; i < mCount; ++i) {
final View mChild = mContainer.getChildAt(i);
if (mChild instanceof TextView) {
// Set the font if it is a TextView.
((TextView) mChild).setTypeface(mFont);
}
if (mChild instanceof EditText) {
// Set the font if it is a TextView.
((EditText) mChild).setTypeface(mFont);
} else if (mChild instanceof ViewGroup) {
// Recursively attempt another ViewGroup.
setAppFont((ViewGroup) mChild, mFont);
}
}
}
//para el font
public static void configFooterFontFragment(View view, Activity activity) throws Exception {
setFontFrag(view,activity, R.id.footer_title,SANTOR);
//setFontFrag(view,activity, R.idLocal.footer_subtitle_1,HELVETICA_BOLD);
//setFontFrag(view,activity, R.idLocal.footer_subtitle_2,HELVETICA_BOLD);
}
public static void setFont(View mView, Context context,String fontName) {
Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName);
if (mView instanceof TextView) {
// Set the font if it is a TextView.
((TextView) mView).setTypeface(typeFace);
return;
} else if (mView instanceof EditText) {
// Set the font if it is a TextView.
((EditText) mView).setTypeface(typeFace);
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment