Apply custom Font to TabPageIndicator using custom TextView
Opened this issue · 0 comments
So this is not an issue ViewPagerIndicator, its a question I had while trying to work around the Android memory leak associated with setting custom fonts. Apologize before hand if this has been covered, or if it does not belong here.
So I have been able to set a CustomFont on TabPageIndicator using setTypeface.
And it works Fine. I have also been able to set a stroke, and different styling attributes within TabPageIndicator.
public void onDraw(Canvas canvas){
final ColorStateList textColor = getTextColors();
TextPaint paint = this.getPaint();
paint.setStyle(Style.STROKE);
paint.setStrokeJoin(Join.ROUND);
paint.setStrokeMiter(10);
this.setTextColor(strokeColor);
paint.setStrokeWidth(3);
super.onDraw(canvas);
//paint.setStyle(Style.FILL);
setTextColor(textColor);
super.onDraw(canvas);
}
This works, but I notice that the tab scrolling action becomes real sluggish when I apply a stroke, gradient to the custom font.
So I figured I would try to implement a custom TextView font for TabPageIndicator, the way I normally use and set Custom fonts, style and subclass.
A CustomFontHelper class
public class VpiCustomFontHelper {
public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
String font = a.getString(R.styleable.CustomFont_font);
setCustomFont(textview, font, context);
a.recycle();
}
public static void setCustomFont(TextView textview, String font, Context context) {
if(font == null) {
return;
}
Typeface tf = FontCache.get(font, context);
if(tf != null) {
textview.setTypeface(tf);
}
}
}
A Font Cache
public class FontCache {
private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();
public static Typeface get(String name, Context context) {
Typeface tf = fontCache.get(name);
if(tf == null) {
try {
tf = Typeface.createFromAsset(context.getAssets(), name);
}
catch (Exception e) {
return null;
}
fontCache.put(name, tf);
}
return tf;
}
}
Custom Font class
public class ScoreTexto extends TextView {
private int strokeColor = 0xff4081ff;
private Paint textPaint = new Paint(Paint.FAKE_BOLD_TEXT_FLAG);
public ScoreTexto(Context context) {
super(context);
}
public ScoreTexto(Context context, AttributeSet attrs) {
super(context, attrs);
VpiCustomFontHelper.setCustomFont(this, context, attrs);
}
//Override
public void onDraw(Canvas canvas)
{
final ColorStateList textColor = getTextColors();
TextPaint paint = this.getPaint();
paint.setStyle(Style.STROKE);
//paint.setStrokeJoin(Join.ROUND);
paint.setStrokeMiter(10);
this.setTextColor(strokeColor);
paint.setStrokeWidth(6);
super.onDraw(canvas);
paint.setStyle(Style.FILL);
setTextColor(textColor);
super.onDraw(canvas);
}
public ScoreTexto(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
VpiCustomFontHelper.setCustomFont(this, context, attrs);
/* parseAttributes(context.obtainStyledAttributes(attrs,
R.styleable.CustomFont));*/
}
}
in styles.xml
<style name="scoretexto" parent="@android:style/Widget.TextView">
<item name="com.example.mypackage:font">fonts/adv_copy.TTF</item>
</style>
in attrs.xml
<declare-styleable name="CustomFont">
<attr name="font" format="string"/>
</declare-styleable>
And then I just use in the layout I want to use the text my_layout.xml
<com.example.mypackage.ScoreTexto
style="@style/scoretexto"
android:id="@+id/chapter_counter"
android:layout_width="180dp"
android:layout_height="180dp"
android:textSize="28dp"
android:textStyle="bold"/>
So my approach was to apply .ScoreTexto's style style="@style/scoretexto" to TabPageIndicator.
So tried styles.xml
<style name="StyledIndicators" parent="@android:style/Theme.Holo">
<item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item> </style>
<style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">
<item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item>
<style name="Widget.TabPageIndicator" parent="Widget">
<item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item>
<style name="CustomTabPageIndicator.Text" parent="@android:style/Widget.TextView">
<!-- <item name="android:typeface">monospace</item> -->
<item name="com.example.mypackage:font">@style/ont</item>
<style name="font" parent="@android:style/Widget.TextView"> -->
<item name="com.example.mypackage.app:font">fonts/adv_copy.TTF</item>
</style>
in attrs.xml
<declare-styleable name="CustomFont">
<attr name="font" format="string"/>
</declare-styleable>
so I figured style="@style/StyledIndicators" would set the custom font in Simple_tabs.xml
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/vpi__background_holo_light"
style="@style/StyledIndicators"/>
But that didnt work, so then I tried
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/vpi__background_holo_light"
style="@style/StyledIndicators">
<com.viewpagerindicator.ScoreTexto
style="@style/scoretexto
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/vpi__background_holo_light"
style="@style/StyledIndicators">
</com.viewpagerindicator.TabPageIndicator>
And I got an error on log cat regarding HorizontalScrollView not allowing more than one child..which makes sense..and was the wrong approach..
Being pretty new with Android I started shooting in the dark to see what would work, so tried extending ScoreTexto.java in TabPageIndcator's public class TabView,
and testing by R.attr.vpiTabPageIndicatorStyle to all the R.attr. I could find..
private class TabView extends ScoreTexto {
private int mIndex;
private int strokeColor = 0xffff9c00;
private Paint textPaint = new Paint(Paint.FAKE_BOLD_TEXT_FLAG);
public TabView(Context context) {
super(context, null, R.attr.vpiTabPageIndicatorStyle);///vpiTabPageIndicatorStyle
}
public TabView(Context context, AttributeSet attrs) {
super(context, null, R.attr.vpiTabPageIndicatorStyle);///vpiTabPageIndicatorStyle
VpiCustomFontHelper.setCustomFont(this, context, attrs);
}
public TabView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
VpiCustomFontHelper.setCustomFont(this, context, attrs);
//parseAttributes(context.obtainStyledAttributes(attrs,
//R.styleable.CustomFont));
}
Some of these test(like the one above) had not effect, and the times my shot in the dark tests did have ScoreTexto set the font and stroke in private Class TabView, it was still sluggish.
So my questions are
Can this be done using this approach?
Am I going about in totally the wrong way?
Is my uber android noobness keeping me from applying this approach?
Thanks in advance