java - How to set the font from Styles.xml in android -
what have:
- i have custom class inheriting
appcompattextview
. - i have defined custom attribute
textformat
inattires.xml
, passing font need set xml
stylefile
<style name="headerfiltername"> <item name="android:src">@drawable/back_button</item> <item name="android:text">@string/str_filter_edit</item> <item name="android:gravity">center</item> <item name="android:textsize">@dimen/header_filter_name_text_size</item> <item name="android:layout_weight">1</item> </style>
xml
<customviews.customtfttextview android:id="@+id/txtscreennameid" android:layout_width="wrap_content" android:layout_height="wrap_content" app:textformat="fonts/sf_san_fransisco.ttf" style="@style/headerfiltername"/>
attr.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="customfont"> <attr name="textformat" format="string"/> </declare-styleable> </resources>
customtfttextview.java
public class customtfttextview extends appcompattextview { private string text; public customtfttextview(final context context) { this(context, null); initialize(text,context); } public customtfttextview(final context context, final attributeset attrs) { this(context, attrs, 0); text = context.getresources().obtainattributes(attrs, r.styleable.customfont).getstring(r.styleable.customfont_textformat); initialize(text,context); } public customtfttextview(final context context, final attributeset attrs, final int defstyle) { super(context, attrs, defstyle); text = context.getresources().obtainattributes(attrs, r.styleable.customfont).getstring(r.styleable.customfont_textformat); initialize(text,context); } private void initialize(string format, context context) { typeface mtypeface; if (format != null) { mtypeface = typeface.createfromasset(context.getassets(), format); } else { mtypeface = typeface.createfromasset(context.getassets(), "fonts/sf_san_fransisco.ttf"); } settypeface(mtypeface, typeface.normal); setlinespacing(0.0f, 1.4f); } }
while above code works perfect, if move app:textformat
inside style
file, font not setting.
<style name="headerfiltername"> <item name="android:src">@drawable/back_button</item> <item name="android:text">@string/str_filter_edit</item> <item name="textformat">@string/custom_font_medium </item> <item name="android:gravity">center</item> <item name="android:textsize">@dimen/header_filter_name_text_size</item> <item name="android:layout_weight">1</item> </style>
strings.xml
<string name="custom_font_medium">fonts/sf_san_fransisco.ttf</string>
how achieve this
if check docs resources#obtainattributes()
method, says:
retrieve set of basic attribute values attributeset, not performing styling of them using theme and/or style resources.
to attributes style applied, use context#obtainstyledattributes()
method instead. advisable keep reference return, can recycle()
when done. example:
typedarray = context.obtainstyledattributes(attrs, r.styleable.customfont); text = a.getstring(r.styleable.customfont_textformat); a.recycle();
Comments
Post a Comment