Property "android:textSize" not applied when in custom style - android

I have following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10sp"
android:paddingBottom="10sp">
<ImageView
android:id="#+id/newsImage"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:contentDescription="#string/hello_world"
android:src="#drawable/ic_program_blue" />
<RelativeLayout
android:id="#+id/textLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/newsImage">
<TextView
android:id="#+id/titleText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
style="#style/newsListItemTitle" />
<TextView
android:id="#+id/shortText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/titleText"
android:text="#string/hello_world"
style="#style/newsListItemShort" />
</RelativeLayout>
</RelativeLayout>
And this style definition:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="#android:style/Theme.Holo.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- NAVIGATION DRAWER -->
<!-- Items -->
<style name="navDrawerItem">
<item name="android:minHeight">48dp</item>
<item name="android:textSize">22sp</item>
<item name="android:textColor">#686868</item>
<item name="android:paddingRight">40dp</item>
<item name="android:gravity">center_vertical</item>
</style>
<!-- NEWS -->
<!-- Item title -->
<style name="newsListItemTitle">
<item name="android:singleLine">true</item>
<item name="android:background">#66ffffff</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">23sp</item>
</style>
<!-- Item short -->
<style name="newsListItemShort">
<item name="android:singleLine">true</item>
<item name="android:background">#66ffffff</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:textColor">#686868</item>
<item name="android:textSize">15sp</item>
</style>
</resources>
Everything gets applied to the TextViews, except the "textSize". No text does appear in the configured textSize height. Any idea, why this is not working?
No compile errors.
When I apply the property "textSize" with the TextView directly everythink works fine.
Thanks

I made the corrections to the wrong "styles.xml" file. I have a styles file for API larger than 16. And this file gets used. But I edited the standard "styles.xml".

Try to add in the below folder structure:
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
Example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>

Related

Button background color is not showing in my android activity

I created a calculator activity as a part of a bigger app, and created a theme, and style for different components. yet, when I assign a background color of orange to the "buttonOperator" styles it doesn't show in the activity on the buttons that are using this style, appreciate explaining to me what i did wrong and maybe how/what tools can be used to troubleshoot such layout problems in android studio if such tools or methods are available.
below is the code relevant to the issue:
calculatorActivity.xml
<\?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/light_Black"
android:orientation="vertical"
android:theme="#style/Theme.UnitConverterAdvance.Calculator"
tools:context=".CalculatorActivity">
<LinearLayout style="#style/buttonRow">
<Button
style="#style/buttonNumber"
android:text="#string/button_7" />
<Button
style="#style/buttonNumber"
android:text="#string/button_8" />
<Button
style="#style/buttonNumber"
android:text="#string/button_9" />
<Button
style="#style/buttonOperator"
android:text="#string/button_division" />
</LinearLayout>
<LinearLayout style="#style/buttonRow">
<Button
style="#style/buttonNumber"
android:text="#string/button_0" />
<Button
style="#style/buttonNumber"
android:text="#string/button_dot" />
<Button
style="#style/buttonOperator"
android:text="#string/button_equal" />
<Button
style="#style/buttonOperator"
android:text="#string/button_plus" />
</LinearLayout>
</LinearLayout>
Style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="buttonRow">
<item name="android:layout_weight">1</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">0dp</item>
</style>
<style name="buttonNumber">
<item name="android:layout_weight">1</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">#null</item>
<item name="android:textSize">25sp</item>
<item name="android:textColor">#color/white</item>
</style>
<style name="buttonOperator">
<item name="android:layout_weight">1</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="backgroundColor">#color/orange</item>
<item name="android:textSize">25sp</item>
<item name="android:textColor">#color/white</item>
</style>
</resources>
Theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.UnitConverterAdvance" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="Theme.UnitConverterAdvance.Calculator" parent="Theme.AppCompat">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/light_Black</item>
<item name="colorPrimaryVariant">#color/orange</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
</style>
</resources>
////Try to give manually in .xml file it. It works fine.
<Button
android:id="#+id/btn_proceToPay"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_below="#id/rl3"
android:layout_marginTop="20dp"
android:background="#color/primaryBackgroung"
android:enabled="false"
android:text="Proceed To Pay"
android:textAllCaps="false"
android:textColor="#color/whiteText"
android:textSize="16sp"
android:textStyle="bold" />
as indicated by commonsware in his comment, the answer is in using the backgroundTint instead of the backgroundColor in the Style.xml. or changing 'button' tag into 'android.widget.Button'.
Because since Android Studio 4.1 any Button elements in a layout get turned into MaterialButton widgets, which ignores background attribute.
for more details refer to commonsware answer

Material Design and appcompat not working on older API versions

I am using appcompat v7 (23.1.1) for a MaterialDesign backward compability. On a API 21 device it looks nice. On older API versions nothing of my styles is affecting. Why?
Result in API 21 (goal):
Result in API 17 (bad):
Here is my style.xml inside values folder:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
<!-- customize the color palette -->
<item name="android:colorPrimary">#color/teal200</item>
<item name="android:colorPrimaryDark">#color/teal500</item>
<item name="android:colorAccent">#color/material_green_A200</item>
<item name="android:statusBarColor">#color/teal500</item>
<item name="android:navigationBarColor">#color/teal500</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:colorButtonNormal">#color/button</item>
<item name="colorControlNormal">#color/deeporange300</item>
<item name="colorControlActivated">#color/deeporange500</item>
<item name="colorControlHighlight">#color/material_green_A200</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are specific to a particular API-level befor 21 can go here. -->
</style>
Here is the styles.xml inside values-v21 folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Customize your theme using Material Design here. -->
<item name="android:buttonStyle">#style/button</item>
<item name="android:buttonStyleToggle">#style/togglebutton</item>
</style>
<style name="button" parent="#android:style/Widget.Material.Button">
<item name="android:textColor">#color/buttontextcolor</item>
</style>
<style name="togglebutton" parent="#android:style/Widget.Material.Button.Toggle">
<item name="android:textColor">#color/buttontextcolor</item>
</style>
</resources>
And here is the layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
tools:context="com.skymedium.theblowdryer.MainActivity"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/menue"
android:id="#+id/menue" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:id="#+id/row1" >
<Button
android:id="#+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/jukebox" />
<RelativeLayout
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.5"
android:id="#+id/subrow" >
<ImageView
android:id="#+id/tooltip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_alignParentRight="true"
android:scaleType="fitCenter"
android:src="#drawable/abc_ab_share_pack_holo_dark" />
</RelativeLayout>
</LinearLayout>/
<LinearLayout
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/row2" >
<ToggleButton
android:id="#+id/onoff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ToggleButton"
android:layout_marginRight="5dp"
android:layout_weight="0.7" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="0.3" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/swipearea" >
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_gravity="top"
android:src="#drawable/abc_ab_share_pack_holo_dark" />
</LinearLayout>
</LinearLayout>
I tried to set <item name="colorButtonNormal">#color/button_color</item>
It doesnt work too on older APIs.
Thanks in advance!
You have it backwards. Let me explain,
styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
<!-- customize the color palette -->
<!--Notice!!! No "android" prefix!-->
<item name="colorPrimary">#color/teal200</item>
<item name="colorPrimaryDark">#color/teal500</item>
<item name="colorAccent">#color/material_green_A200</item>
<item name="android:statusBarColor">#color/teal500</item>
<item name="android:navigationBarColor">#color/teal500</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:colorButtonNormal">#color/button</item>
<item name="colorControlNormal">#color/deeporange300</item>
<item name="colorControlActivated">#color/deeporange500</item>
<item name="colorControlHighlight">#color/material_green_A200</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are specific to a particular API-level befor 21 can go here. -->
</style>
styles-v21
<resources>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Customize your theme using Material Design here. -->
<!--Be sure to define your colors here as well! but this time use "android:"-->
<item name="android:colorPrimary">#color/teal200</item>
<item name="android:colorPrimaryDark">#color/teal500</item>
<item name="android:colorAccent">#color/material_green_A200</item>
<item name="android:buttonStyle">#style/button</item>
<item name="android:buttonStyleToggle">#style/togglebutton</item>
</style>
<style name="button" parent="#android:style/Widget.Material.Button">
<item name="android:textColor">#color/buttontextcolor</item>
</style>
<style name="togglebutton" parent="#android:style/Widget.Material.Button.Toggle">
<item name="android:textColor">#color/buttontextcolor</item>
</style>
</resources>
styles.xml for < API 21 should not use the "android" prefix for your colors.
in styles.xml v-21 define your colors WITH the android prefix. The XML above should work!

Why android:buttonStyle does not style buttons?

So, I have this weird issue: My app is very simple, main activity with just one button in it and custom theme for the activity set in the manifest.
I can confirm the theme works and is selected, as I can change activity background or font color e.g.
But when I try to style all Buttons on my activity, it does not work!
This is the styles.xml code I use:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:background">#0f0</item>
<item name="android:buttonStyle">#style/btxs</item>
</style>
<style name="btxs" parent="#android:style/Widget.Button">
<item name="android:textColor">#f00</item>
</style>
</resources>
Background of activity is green (so theme works), but button is still default.
Is android:buttonStyle correct way of setting button style in theme?
for reference I paste also my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_toEndOf="#+id/textView"
android:layout_marginTop="25dp" />
</RelativeLayout>
An AppCompatActivity will use buttonStyle. I'm also assuming that you probably want to extend Base.Widget.AppCompat.Button.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:background">#0f0</item>
<item name="buttonStyle">#style/btxs</item>
</style>
<style name="btxs" parent="Base.Widget.AppCompat.Button">
<item name="android:textColor">#f00</item>
</style>
</resources>

Android Layout xml file should derive all properties of a view from styles.xml

This is my layout.xml derives all view parameters from styles.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
android:layout_width="match_parent"
style="#style/DataContainer">
<ImageView
android:id="#id/df_flight_logo"
style="#style/FlightLOGO"
/>
<TextView
android:id="#id/flight_name"
style="#style/FlightName"
/>
<TextView
android:id="#id/flight_price"
style="#style/FlightPrice"
/>
</RelativeLayout>
This is my styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="DataContainer">
<item name="android:layout_height">125dp</item>
<item name="android:background">#ffffff</item>
</style>
<style name="FlightLOGO">
<item name="android:layout_height">25dp</item>
<item name="android:layout_weight">25dp</item>
<item name="android:background">#drawable/background</item>
</style>
<style name="FlightName">
<item name="android:layout_height">32dp</item>
<item name="android:background">#b2b2b2</item>
</style>
<style name="FlightPrice">
<item name="android:layout_height">12dp</item>
<item name="android:layout_marginLeft">18dp</item>
<item name="android:background">#b2b2b2</item>
</style>
</resources>
So Should i place all view attributes(width,height,margin,padding) to styles.xml? or properties for specific view(button styles, text styles) to styles.xml?
According to your comment explaining what you are trying to do, you should use dimens instead. They are resources like strings.
<resources>
<dimen name="title_size">32sp</dimen>
</resources>
And for example usage with TextView
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/title_size"/>
Now you have different dimens.xml files for different screen sizes, but layout stays the same!
An example is here
http://android4beginners.com/2013/07/appendix-c-everything-about-sizes-and-dimensions-in-android/

how to call a another layout file inside a styles in android?

i custom the Theme in android Widget.TabWidget android:tablayout
and overide the style of tabwidget in my ownway not using #android because
i want to custom my style of API 10 below same like API11+
<resources>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="AppBaseTheme" parent="android:Theme">
<!-- Window -->
<item name="android:windowNoTitle">true</item>
<item name="android:tabWidgetStyle">#style/MyTabWidget</item>
</style>
<!-- TabWidget -->
<style name="MyTabWidget" parent="android:style/Widget.TabWidget">
<item name="android:textAppearance">#style/MyTabWidgetTextAppearance</item>
<item name="android:gravity">center|center</item>
<item name="android:tabLayout"> HOW TO CALL api_1_10_indicator.xml LAYOUT FROM MY RES LAYOUT </item>
</style>
<!-- TabWidget TextAppearance -->
<style name="MyTabWidgetTextAppearance" parent="android:style/TextAppearance.Widget.TabWidget">
<item name="android:textSize">14sp</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">#color/select_tabwidget_text_color</item>
</style>
</resources>
my res/layout/api_1_10_indicator.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="48dip"
android:layout_weight="1"
android:layout_marginStart="-3dip"
android:layout_marginEnd="-3dip"
android:background="#color/api_1_10_tab_indicator">
<ImageView android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="?android:attr/tabWidgetStyle"
/>
</RelativeLayout>
if i use <item name="android:tablayout">#layout/api_1_10_indicator</item>
its not working icant call my own tablayout for the tabwidget that i custom.
HOW TO DO IT?

Categories

Resources