Android font is not applied for button via style - android

I created style for Button
<style name="MainButtonStyle">
<item name="android:background">#drawable/btn_main_selector</item>
<item name="android:textColor">#color/btn_main_text_color_selector</item>
<item name="android:minHeight">40dp</item>
<item name="android:textSize">18sp</item>
<item name="android:gravity">center</item>
<item name="android:textAppearance">#font/bold</item>
</style>
This style applies in the AppTheme
<item name="android:buttonStyle">#style/MainButtonStyle</item>
bold.ttf file is in res/font folder
If I run an app I still see the default font. Where is the catch?

textAppearance allows you to define text-specific styling: font family, font color, size, etc. It is not for referencing font resource.
Just use the fontFamily attribute to set the font in xml:
<style name="MainButtonStyle">
...
<item name="fontFamily">#font/bold</item>
</style>
or
<style name="MainButtonTextAppearance">
<item name="android:textColor">#color/btn_main_text_color_selector</item>
<item name="android:textSize">18sp</item>
<item name="fontFamily">#font/bold</item>
</style>
<style name="MainButtonStyle">
<item name="android:background">#drawable/btn_main_selector</item>
<item name="android:minHeight">40dp</item>
<item name="android:gravity">center</item>
<item name="android:textAppearance">#Style/MainButtonTextAppearance</item>
</style>

make your button style inherit from parent Button style like
<style name="MainButtonStyle" parent="Widget.AppCompat.Button">

Try this :
<style name="MainButtonStyle">
<item name="android:background">#drawable/btn_main_selector</item>
<item name="android:textColor">#color/btn_main_text_color_selector</item>
<item name="android:minHeight">40dp</item>
<item name="android:textSize">18sp</item>
<item name="android:gravity">center</item>
<item name="fontFamily">#font/bold</item>
</style>

Related

Styling OptionMenu on android

Here is the default Option Menu style in android. Currently I tried.
My question is : I want to override the default to style of Option Menu. Like this!
which is only first character is cap and custom typeface.
Here is my implememtation.
//style.xml
<style name="HomeTheme" parent="AppTheme.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
<item name="android:fontFamily">#font/apercu</item>
<item name="actionBarStyle">#style/AppTheme.ActionBar</item>
</style>
<style name="AppTheme.ActionBar" parent="ThemeOverlay.AppCompat.ActionBar">
<item name="android:actionMenuTextAppearance">#style/TextAppearance.Menu</item>
</style>
<style name="TextAppearance.Menu" parent="TextAppearance.AppCompat.Widget.ActionBar.Menu">
<item name="android:fontFamily">#font/custom_type_face</item>
<item name="android:textSize">17sp</item>
<item name="android:textColor">#4d4d4d</item>
<item name="android:textAllCaps">false</item>
</style>
I accomplished it overriding AppTheme.Widget.ActionButton. Eg.
<style name="AppTheme.Widget.ActionButton" parent="#style/Widget.AppCompat.ActionButton">
<item name="textAllCaps">false</item>
</style>
and set it to my Activity's theme like
<item name="actionButtonStyle">#style/AppTheme.Widget.ActionButton</item>

Android : Set fontFamily in TextAppearance not working

I wanted to have a default Headline TextStyle with fonts, and every style(HeaderStyle, HeaderTwoStyle) inheriting should be able to display correct font, in case I wanted to override the font in Headline. But its not working...
<style name="Headline1" parent="TextAppearance.AppCompat.Headline">
<item name="android:textSize">#dimen/font_28sp</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textColor">#color/headline1</item>
</style>
<style name="HeaderStyle">
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:gravity">center</item>
<item name="android:textAppearance">#style/Headline1</item>
</style>
<style name="HeaderTwoStyle">
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:gravity">center</item>
<item name="android:textAppearance">#style/Headline1</item>
</style>
You should write like <style name="HeadLine1.HeaderStyle"> to override style in the same file

Parent style for TextView style with AppCompat usage

What parent style should I use if I want to set global textViewStyle and maintain backward compatibility with AppCompat? I can't find something like Widget.AppCompat.TextView :
<style name="Base_AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textViewStyle">#style/GlobalTextViewStyle</item>
</style>
<style name="GlobalTextViewStyle" parent="?????">
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Medium</item>
</style>
You just need to extend TextAppearance.AppCompat style overriding necessary values:
<style name="Base_AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textAppearance">#style/MyTextAppearance</item>
</style>
<style name="MyTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">?android:textColorPrimary</item>
<item name="android:textColorHint">?android:textColorHint</item>
<item name="android:textColorHighlight">?android:textColorHighlight</item>
<item name="android:textColorLink">?android:textColorLink</item>
<item name="android:textSize">#dimen/abc_text_size_body_1_material</item>
<item name="textColor">?textColorPrimary</item>
<item name="textColorHighlight">?textColorHighlight</item>
<item name="textColorHint">?textColorHint</item>
<item name="textColorLink">?textColorLink</item>
<item name="textSize">16sp</item>
<item name="textStyle">normal</item>
</style>
If you want to set this style for all your TextView's in your app, you can do something like this:
<resources>
<style name="YourTheme" parent="android:YourParentThTheme">
<item name="android:textViewStyle">#style/GlobalTextViewStyle </item>
</style>
<style name="GlobalTextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">#FF000</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
<style name="normal_text_white" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#color/white_color</item>
<item name="android:textSize">#dimen/small_text_size</item>
<item name="android:textColorHint">#color/hint_color</item>
</style>
We can try this as well for AppCompat widget.
<style name="TitleStyle" parent="TextAppearance.AppCompat">
<item name="android:gravity">center_horizontal</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:textSize">26sp</item>
</style>

How to inherit system defined style in my style file in android?

I customize a style named MyDialogWindowTitle in my style file, I hope this style inherited from system style Widget.TextView.ListSeparator,
butthe code parent="#style/Widget.TextView.ListSeparator" cause an error, how can I do ? Thanks!
<style name="MyDialogWindowTitle" parent="?android: style/Widget.TextView.ListSeparator">
<item name="android:textSize">30sp</item>
</style>
<style name="Widget.TextView.ListSeparator">
<item name="android:background">#android:drawable/dark_header_dither</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">25dip</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">?textColorSecondary</item>
<item name="android:textSize">14sp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:paddingLeft">5sp</item>
</style>
Instead of parent="?android: style/Widget.TextView.ListSeparator", use parent="#style/Widget.TextView.ListSeparator"
This is because Widget.TextView.ListSeparator is defined by you and is not defined in android's libraries

styling drop down/navigation bar actionbarsherlock font

In the application I have, I would like to have the actionbar drop down's font to be white. Till now I have the following style:
<style name="Theme.MyApplications" parent="#style/Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/ActionBar.Solid</item>
<item name="android:textSize">18sp</item>
</style>
<style name="ActionBar.Title" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="ActionBar.SubTitle" parent="TextAppearance.Sherlock.Widget.ActionBar.Subtitle">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="ActionBar.Solid" parent="#style/Widget.Sherlock.Light.ActionBar.Solid">
<item name="titleTextStyle">#style/ActionBar.Title</item>
<item name="subtitleTextStyle">#style/ActionBar.SubTitle</item>
</style>
This style gives me a black colored font in android 3.0 and higher. Can someone please help me configure the font to be white ?
The funny thing about this is that the action overflow popup's font appears in white.
for now I just used the simple
<style name="Theme.MyApplications" parent="#style/Theme.Sherlock">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#FFFFFF</item>
</style>
Its simple but it got the job done. And what got the job done was the change in the style's parent.
You have apply the style to the native ActionBar as well:
<style name="Theme.MyApplications" parent="#style/Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/ActionBar.Solid</item>
<item name="android:actionBarStyle">#style/ActionBar.Solid</item>
<item name="android:textSize">18sp</item>
</style>
<style name="ActionBar.Title" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="ActionBar.SubTitle" parent="TextAppearance.Sherlock.Widget.ActionBar.Subtitle">
<item name="android:textColor">#android:color/white</item>
</style>
<style name="ActionBar.Solid" parent="#style/Widget.Sherlock.Light.ActionBar.Solid">
<item name="titleTextStyle">#style/ActionBar.Title</item>
<item name="subtitleTextStyle">#style/ActionBar.SubTitle</item>
<item name="android:titleTextStyle">#style/ActionBar.Title</item>
<item name="android:subtitleTextStyle">#style/ActionBar.SubTitle</item>
</style>

Categories

Resources