In my project I am using the font android: fontFamily = "sans-serif-light", and working properly.
I am also using the library viewpagerindicator. I want to use the font android: fontFamily = "sans-serif-light" also in the viewpagerindicator, but can not find how to do it
I've tried using android:fontFamily = "sans-serif-light" in <com.viewpagerindicator.TitlePageIndicator ... and in style, but without success.
I have also tried:
PageIndicator mIndicator = (TitlePageIndicator) findViewById (R.id.indicator);
Typeface myTypeface = Typeface.createFromAsset (getAssets (), "fonts / Roboto-Light.ttf");
mIndicator.setTypeface (myTypeface);
but this does not work ..
I appreciate any help.
Thanks and regards
If I don't get you wrong, you want to change titles font in view pager indicator,
I changed the library to achieve that,
for TabPageIndicator custome typeface I added this for TabPageIndicator.java
private Typeface mTypeface;
public void setTypeFace(Typeface tf) {
this.mTypeface = tf;
}
and then change addTab function to this:
private void addTab(int index, CharSequence text, int iconResId) {
final TabView tabView = new TabView(getContext());
tabView.mIndex = index;
tabView.setFocusable(true);
tabView.setOnClickListener(mTabClickListener);
tabView.setText(text);
**if (mTypeface != null) {
tabView.setTypeface(mTypeface);
}**
if (iconResId != 0) {
tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
}
mTabLayout.addView(tabView, new LinearLayout.LayoutParams(0, MATCH_PARENT, 1));
}
now you should just setTypeFace on your tabpagerindicator, like this:
mTabPageIndicator = (TabPageIndicator) findViewById(R.id.tab_page_indicator);
mTabPageIndicator.setTypeFace(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/custome_font.ttf");
I'm using the ViewPageIndicator library, here's how I did it (index is the tab index in your view page indicator):
private void changeTabText(int index, boolean on){
if(tabLayout.getChildCount()-1 >= index){
TextView tv = (TextView)tabLayout.getChildAt(index);
tv.setTextColor(getResources().getColor(on? android.R.color.black : R.color.light_grey));
CustomFont.setTypeface(tv, CustomFont.NORMAL);
}
}
Here's how I got the tablayout:
tabLayout = (ViewGroup)indicator.getChildAt(0); //indicator is a horizontal scroll view, there will be only one root layout
Here's what my custom font does:
public static void setTypeface(TextView view, String font) {
Typeface typeface = Typeface.createFromAsset(view.getContext().getAssets(), BASE_PATH + font);
view.setTypeface(typeface);
}
A more OOD implementation is to modify the Library by creating a new interface:
public interface FontPagerAdapter {
/**
* Get the fonts to set.
*/
Typeface getCustomFont();}
And in class TabPageIndicator.java add a new property:
private Typeface customTypeFace;
which will be set in the notifyDataSetChanged() method by declaring:
if (adapter instanceof FontPagerAdapter) {
FontPagerAdapter fontAdapter = (FontPagerAdapter)adapter;
customTypeFace = fontAdapter.getCustomFont();
}
Later you would change the Font by setting it programatically in the addTab method, just by adding:
if (customTypeFace != null) {
tabView.setTypeface(customTypeFace);
}
Finally in the adapter that will use the library, you need to implement this interface, then override the method:
#Override
public Typeface getCustomFont() {
Typeface font = Typeface.createFromAsset(context.getAssets(),"fonts/PoetsenOne-Regular.ttf");
return font;
}
Related
With the previous versions of AppCompat it was easy to get the ActionBar's title TextView and modify it.
There is the method I was using:
private TextView getActionBarTitleView() {
int id = getResources().getIdentifier("action_bar_title", "id", "android");
return (TextView) findViewById(id);
}
And then to change the alpha's value of the title:
getActionBarTitleView().setAlpha(ratio*255);
Now for some reasons, "action_bar_title" with the lastest AppCompat version isn't working anymore. When I tried to use my method, it returned me "null". Tried to use other id's of the ActionBar but I didn't find the good one.
I saw 1 post on StackOverflow from Ahmed Nawara and the only way he found for the moment was to do an Iteration over the Toolbar's children views and whenever a TexView is found, compare it's text value with the toolbar.getTitle() to make sure that's the TexView we're looking at.
If someone could help me to integrate this solution in my case because I don't know how to do it actually.
I guess you got your method from Flavien Laurent's post on making the ActionBar not boring. If you take a closer look, he detailled another technique to set the ActionBar title's alpha inspired by Cyril Mottier.
It uses a custom AlphaForegroundColorSpan class that extends ForegroundColorSpan :
public class AlphaForegroundColorSpan extends ForegroundColorSpan
{
private float mAlpha;
public AlphaForegroundColorSpan(int color)
{
super(color);
}
public AlphaForegroundColorSpan(Parcel src)
{
super(src);
mAlpha = src.readFloat();
}
public void writeToParcel(Parcel dest, int flags)
{
super.writeToParcel(dest, flags);
dest.writeFloat(mAlpha);
}
#Override
public void updateDrawState(TextPaint ds)
{
ds.setColor(getAlphaColor());
}
public void setAlpha(float alpha)
{
mAlpha = alpha;
}
public float getAlpha()
{
return mAlpha;
}
private int getAlphaColor()
{
int foregroundColor = getForegroundColor();
return Color.argb((int) (mAlpha * 255), Color.red(foregroundColor), Color.green(foregroundColor), Color.blue(foregroundColor));
}
}
Then, using a SpannableString, you just set the alpha to the AlphaForegroundColorSpan, and then set this AlphaForegroundColorSpan to the SpannableString :
public void onCreate(Bundle savedInstanceState)
{
...
spannableString = new SpannableString("ActionBar title");
alphaForegroundColorSpan = new AlphaForegroundColorSpan(0xffffffff);
...
}
private void setActionBarTitle(int newAlpha)
{
alphaForegroundColorSpan.setAlpha(newAlpha);
spannableString.setSpan(alphaForegroundColorSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(spannableString);
}
Hope it helps. If it's not clear enough, give another look to Flavient Laurent's post!
With AppCompat you should use the new toolbar including the toolbar.xml in the activity layout and importing android.support.v7.widget.Toolbar.
In your activity, OnCreate you will have:
mtoolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mtoolbar);
getActionBarTitleView().setAlpha(ratio*255);
at this point you are almost done and you can use reflection to access to the view (remember to import java.lang.reflect.field;) and your function will be:
private TextView getActionBarTitleView() {
TextView yourTextView = null;
try {
Field f = mToolBar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
yourTextView = (TextView) f.get(mToolBar);
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
return yourTextView;
}
I have the following code in my Activity to change the font for some ViewGroup in a layout:
setFont("fonts/texgyreheros-regular-webfont.ttf", listBold, TypefaceStyle.Bold);
The definition of setFont is as follows:
private void setFont(string path, List<TextView> tTV, TypefaceStyle Type)
{
Android.Content.Res.AssetManager mgr;
mgr = Assets;
Typeface font = Typeface.CreateFromAsset(mgr, path);
for (int i = 0; i < tTV.Count; i++)
{
tTV[i].SetTypeface(font, Type);
}
}
listBold is a List<TextView> which is populated by calling:
findViewById<TextView>(Resource.Id.(...));
multiple times.
Is there any way to avoid this step by setting the default font in Android manifest.xml or somwhere else?
You can create a custom TextView with your font from assets.
public class TextViewWithFont : TextView {
private const string FONT = "fonts/font.ttf";
public TextViewWithFont(Context context) : base(context) {
SetTypeface(Typeface.CreateFromAsset(context.Assets, FONT));
}
}
Then use this class in layout.
<com.example.views.TextViewWithFont
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
I'm trying to apply a custom font style for my app..I know:
Typeface tf = Typeface.createFromAsset(getAssets(),"font.ttf");
text.setTypeface(tf);
But I want to apply custom font style for the entire app including the action bar tabs and all the text view.
is there a "supper" way to apply a custom font for the whole app??
You cannot set a custom font for the whole application in a single point yet, but you have to set the typeface for every view by yourself.
See this answer: Android: Want to set custom fonts for whole application not runtime
Custom ActionBar Styling
ActionBar Tabs
ActionBar Title
There is no way to set an entire applications typeface, but if you are looking for a more general programatic solution, I created a static class that can be used to set the Typeface of an entire view (Activity UI). Note that I am working with Mono (C#) but you can implement it easily using Java.
You can pass this class a layout or a specific view that you want to customize. If you want to be super efficient you could implement it using the Singleton pattern.
public static class AndroidTypefaceUtility
{
static AndroidTypefaceUtility()
{
}
//Refer to the code block beneath this one, to see how to create a typeface.
public static void SetTypefaceOfView(View view, Typeface customTypeface)
{
if (customTypeface != null && view != null)
{
try
{
if (view is TextView)
(view as TextView).Typeface = customTypeface;
else if (view is Button)
(view as Button).Typeface = customTypeface;
else if (view is EditText)
(view as EditText).Typeface = customTypeface;
else if (view is ViewGroup)
SetTypefaceOfViewGroup((view as ViewGroup), customTypeface);
else
Console.Error.WriteLine("AndroidTypefaceUtility: {0} is type of {1} and does not have a typeface property", view.Id, typeof(View));
}
catch (Exception ex)
{
Console.Error.WriteLine("AndroidTypefaceUtility threw:\n{0}\n{1}", ex.GetType(), ex.StackTrace);
throw ex;
}
}
else
{
Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / view parameter should not be null");
}
}
public static void SetTypefaceOfViewGroup(ViewGroup layout, Typeface customTypeface)
{
if (customTypeface != null && layout != null)
{
for (int i = 0; i < layout.ChildCount; i++)
{
SetTypefaceOfView(layout.GetChildAt(i), customTypeface);
}
}
else
{
Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / layout parameter should not be null");
}
}
}
In your activity you will need to create a Typeface object. I create mine in the OnCreate() using a .ttf file placed in my Resources/Assets/ directory. Make sure that the file is marked as an Android Asset in its' properties.
protected override void OnCreate(Bundle bundle)
{
...
LinearLayout rootLayout = (LinearLayout)FindViewById<LinearLayout>(Resource.Id.signInView_LinearLayout);
Typeface allerTypeface = Typeface.CreateFromAsset(base.Assets,"Aller_Rg.ttf");
AndroidTypefaceUtility.SetTypefaceOfViewGroup(rootLayout, allerTypeface);
}
I am trying to assign a different font to my project.
I want the new font is valid for the entire project, but all I find is to change the font to a textview
Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/RockFont.ttf");
TextView customText1 = (TextView)findViewById(R.id.text1);
customText1.setTypeface(font1);
customText1.setTextSize(40.f);
customText1.setText("Hello! This is a custom font...");
There any way to default to the entire project a custom font?
Best regard
Not exactly what you asked for, but building on the comment above there are ways to make using a custom font with default controls easier.
This shows how to extend TextView and use a custom attribute so the TextView supports a custom font.
Custom fonts and XML layouts (Android)
What I do is create a support class and instantiate it from my activity and pass through all the views I wish to style.
public class textfactory{
private TextView tv;
private Button b;
private RadioButton rb;
private TypeFace font;
/**
* fetch font resource
*/
public textfactory(Context context){
this.font = Typeface.createFromAsset(context.getAssets(), 'customfont.ttf');
}
/**
* pass in all the views you wish to apply font to
*/
public void style(View... views){
for(View v : views){
if(v instance of TextView)
{
tv = (TextView)v;
tv.setTypeface(this.font);
}
else if(v instance of Button)
{
b = (Button)v;
b.setTypeface(this.font);
}
else if(v instance of RadioButton)
{
rb = (RadioButton)v;
rb.setTypeface(this.font);
}
//add as many view conditionals as required
}
}
}
I have a ttf file, and its theoretically possible, but I'm not looking to touch over 500 different lines of code to programmatically change this. What is the easiest way?
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Typefaces.html
To extend previous answer with static TYPEFACE:
Add new function:
public static void applyCustomFont(ViewGroup list, Typeface customTypeface) {
for (int i = 0; i < list.getChildCount(); i++) {
View view = list.getChildAt(i);
if (view instanceof ViewGroup) {
applyCustomFont((ViewGroup) view, customTypeface);
} else if (view instanceof TextView) {
((TextView) view).setTypeface(customTypeface);
}
}
}
Get root view of our Layout:
View rootView = findViewById(android.R.id.content)
Than apply custom font for whole activity form with all sub-elements:
applyCustomFont((ViewGroup)rootView, C.TYPEFACE.ArialRounded(this));
I've dealt with this problem myself; although I couldn't set a custom font globally, I was able to just make it little easier to deal with.
So in my Constants class (C.java) I have an Inner class:
public static final class TYPEFACE {
public static final Typeface Helvetica(Context ctx){
Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "helvetica.otf");
return typeface;
}
public static final Typeface ArialRounded(Context ctx){
Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "arial_rounded.ttf");
return typeface;
}
}
And in my code, after declaring and intializing the TextView I just set it's Typeface:
TextView title = (TextView)findViewById(R.id.title);
title.setTypeface(C.TYPEFACE.Helvetica(this));
I know this doesn't solve your problem but I hope it helps...
-serkan
Another way to go would be a custom view extending TextView. Not pretty, but you wouldn't need to copy the font code all over the place.
In all, its a pretty annoying problem for sure.