Reputation: 295
I've seperated my UI part into a lib and am creating my styles there.
I have a default style for all buttons and I'm setting it like this.
<style name="MyTheme.Default">
<item name="buttonStyle">@style/button</item>
</style>
My button style is
<style name="button">
<item name="fontPath">fonts/myfont.ttf</item>
<item name="android:gravity">center</item>
<item name="android:clickable">true</item>
<item name="android:textSize">14sp</item>
</style>
I want this button style to be applied to all buttons without me calling the @style attribute. Everything seems to work fine except for the font. It's not getting applied.
If I call using the @style attribute, it seems to apply the font.
I can apply the font for all the texts in the application using textAppearance, this changes the font of my button as well. but this is behaviour I don't want.
So, how can I apply a custom to buttons through my lib without explicitly using the @style attribute on every button.
Upvotes: 3
Views: 2254
Reputation: 1322
I've found purely theme-based solution (using 2.2.0
version of the library):
styles.xml
Raised button
<style name="YourAppTheme" parent="Theme.AppCompat">
<item name="android:buttonStyle">@style/RaisedButton</item>
</style>
<style name="RaisedButton" parent="Widget.AppCompat.Button.Colored">
<item name="fontPath">fonts/Roboto-Medium.ttf</item>
<!-- extras -->
<item name="android:textAppearance">@style/TextAppearanceButtonColored</item>
</style>
<style name="TextAppearanceButtonColored" parent="TextAppearance.AppCompat.Widget.Button">
<item name="android:textColor">@color/btn_text_selector</item>
</style>
Flat button
<style name="YourAppTheme" parent="Theme.AppCompat">
<item name="android:textAppearanceButton">@style/TextAppearanceButtonBorderless</item>
</style>
<style name="TextAppearanceButtonBorderless" parent="TextAppearance.AppCompat.Widget.Button">
<item name="fontPath">fonts/Roboto-Medium.ttf</item>
<!-- extras -->
<item name="android:textColor">@color/btn_borderless_text_selector</item>
</style>
So there is difference where "fontPath" param for flat and raised buttons should be written to.
Upvotes: 0
Reputation: 13129
I can't fully remember, but we don't pipe in the default styles into Calligraphy if AppCompat is on the Class Path. Subsiquently you will probably need to do this:
new CalligraphyConfig.Builder().setDefaultFontPath("fonts/Roboto-Light.ttf")
.addCustomStyle(AppCompatButton.class, R.attr.buttonStyle)
.setFontAttrId(R.attr.fontPath)
.build());
Take note of the line:
.addCustomStyle(AppCompatButton.class, R.attr.buttonStyle)
This will tell Calligraphy to use the buttonStyle
for AppCompatButton
's by default. Then you can use:
<style name="MyTheme.Default">
<item name="buttonStyle">@style/button</item>
</style>
Upvotes: 6
Reputation:
I think that I've found the problem. If you are using AppCompat the font wont be applied to some widgets like Button, because AppComat replace the components with AppCompat versions when inflating (e.g. Button--->AppCompatButton).
So you can't do do it using only xml. A possible way using Java would be:
private void changeFont(ViewGroup layout) {//Where 'layout' is the parent
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
if (child instanceof ViewGroup) {
changeFont((ViewGroup) child);
} else if (child instanceof Button) {
child.setTypeface(...//The font that you want
}
}
}
Another option, if your minSDK>16 is avoid using AppCompat. I haven't tried this but it shoul work
Hope this helps you.
Upvotes: 3