I have a styles.xml where I have defined styles
<!-- =============================================================== -->
<!-- Dark styles -->
<!-- =============================================================== -->
<style name = "AppBgDark">
<item name="android:background">#333333</item>
</style>
<style name = "CardBgDark">
<item name="android:background">#drawable/rounded_corners_dark</item>
</style>
<style name="event_title_dark">
<item name="android:textColor">#fff</item>
</style>
<style name="event_date_dark">
<item name="android:textColor">#fff</item>
</style>
<style name="event_location_dark">
<item name="android:textColor">#aaa</item>
</style>
<style name="event_calendar_dark">
<item name="android:textColor">#aaa</item>
</style>
<style name="Actionbar_dark" parent= "Widget.Sherlock.Light.ActionBar">
<item name = "android:background">#333333</item>
the same way for a light theme as well.
Then i have created a themes.xml and a attrs.xml to refference these styles.
The problem is when i apply the references from the attrs.xml in the layout i can see no changes happening
For example:
<LinearLayout
android:id="#+id/cardlayout"
style="?card_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:orientation="vertical"
android:padding="7dp" >
where am I going wrong? Any suggestions? I am really confused!!!
Related
I am migrating from M2 to M3 and my basic materialThemeOverlay no longer works. I follow the code at the bottom of the doc closely, it matched pretty much what I had for M2:
styles.xml
<style name="SecondaryThemeOverlay" parent="">
<item name="colorPrimary">#color/md_theme_light_secondary</item>
<item name="colorOnPrimary">#color/md_theme_light_onSecondary</item>
</style>
<style name="Widget.Button.Secondary" parent="Widget.Material3.Button">
<item name="materialThemeOverlay">#style/SecondaryThemeOverlay</item>
</style>
layout.xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Standard Button" />
<Button
style="#style/Widget.Button.Secondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Standard Button Secondary" />
lib version is com.google.android.material:material:1.6.0-beta01, and the Activity is an AppCompatActivity.
App theme parent is Theme.Material3.DayNight.NoActionBar.
Also note that setting the backgroundTint works fine.
You should use ThemeOverlay on materialThemeOverlay like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Button style on both light and night mode -->
<style name="AppButtonStyle" parent="Widget.Material3.Button">
<item name="materialThemeOverlay">#style/ThemeOverlay.App.Button</item>
<item name="android:textAppearance">#style/TextAppearance.App.Button</item>
<item name="shapeAppearanceOverlay">#style/ShapeAppearance.App.SmallComponent</item>
</style>
<!-- Button with text style on both light and night mode -->
<style name="AppButtonTextStyle" parent="Widget.Material3.Button.TextButton">
<item name="materialThemeOverlay">#style/ThemeOverlay.App.Button.TextButton</item>
<item name="android:textAppearance">#style/TextAppearance.App.Button</item>
<item name="shapeAppearanceOverlay">#style/ShapeAppearance.App.SmallComponent</item>
</style>
<!-- Button outline style on both light and night mode -->
<style name="AppOutlinedButtonStyle" parent="Widget.Material3.Button.OutlinedButton">
<item name="materialThemeOverlay">#style/ThemeOverlay.App.Button.OutlinedButton</item>
<item name="android:textAppearance">#style/TextAppearance.App.Button</item>
<item name="shapeAppearanceOverlay">#style/ShapeAppearance.App.SmallComponent</item>
</style>
<style name="ThemeOverlay.App.Button" parent="ThemeOverlay.Material3.Button">
<!-- Background color -->
<item name="colorPrimary">#color/primaryDarkAccent</item>
<!-- Text color -->
<item name="colorOnPrimary">#android:color/white</item>
</style>
<style name="ThemeOverlay.App.Button.TextButton" parent="ThemeOverlay.Material3.Button.TextButton">
<!-- Background color -->
<item name="colorPrimary">#color/primaryDarkAccent</item>
<!-- Text color -->
<item name="colorOnSurface">#android:color/white</item>
</style>
<style name="ThemeOverlay.App.Button.OutlinedButton" parent="ThemeOverlay.Material3.Button">
<!-- Background color -->
<item name="colorOnSurface">#color/primaryDarkAccent</item>
<!-- Text color -->
<item name="colorPrimary">#color/primaryDarkAccent</item>
</style>
<style name="TextAppearance.App.Button" parent="TextAppearance.Material3.LabelLarge">
<item name="fontFamily">sans-serif-medium</item>
<item name="android:fontFamily">sans-serif-medium</item>
</style>
</resources>
Use it like this in you main theme.
<item name="materialButtonStyle">#style/AppButtonStyle</item>
Unfortunately the sample in the documentation seems incorrect as always.
This looks easy enough but i don't know why i'm not able to do this. This is how i want to style my button:
Resource file
<Button
android:layout_marginTop="0dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:id="#+id/buttonLogin"
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ButtonTheme"/>
Styles.xml
<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>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="EditTextTheme">
<item name="colorControlActivated">#color/colorPrimaryDark</item>
</style>
<style name="ButtonTheme">
<item name="android:background">#color/colorPrimary</item>
<item name="android:textColor">#color/colorWhite</item>
</style>
</resources>
But somehow the background color is still default which is grey although the text has already change color to white.
Why did this happen? And how i can overcome this? Thank you in advance.
Instead of
android:theme="#style/ButtonTheme"
Do
style="#style/ButtonTheme"
on your <Button>
I found out that in my app colours are set in res/values/themes.xml so I deleted the styles.xml I created and continued with changes in themes.xml. This worked for me. No styles.xml needed.
Hello I need help on changing the text color of the action Bar the following is my style.xml.I need to change the text color to white including the settings icon.
<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>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
The String.xml source codes are as follows.
<resources>
<string name="app_name">yebo</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="action_settings">Settings</string>
</resources>
try this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyTheme.ActionBarStyle</item>
</style>
<style name="MyTheme.ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#color/red</item>
</style>
</resources>
Change in your style.
<!-- 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>
Add underneath line:
<item name="actionMenuTextColor">Your Color Here</item>
<item name="textColorPrimary">Your Color Here</item>
</style>
Hope it helps.
You can simply put color to your strings like this:
<string name="app_name"><![CDATA[<b><font color=#FFFFFF>yebo</b>]]></string>
In your .axml put a RelativeLayout to add the actionbar. In here add a TextView with the property android:textColor="#FFFFFF". (#FFFFFF is white). As such:
<RelativeLayout
android:id="#+id/titleBarLinearLayoutFrontPage"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:background="#696969">
<TextView
android:textColor="#FFFFFF"
android:id="#+id/txtActionBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Action Bar"
android:layout_gravity="center"
android:clickable="true"
android:layout_centerVertical="true"
android:textSize="22dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="60dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Valus/styles.xml uses colors.xml file. When you press "control" and click #color/colorPrimary it shows you where colorPrimary comes. Which is colors.xml. In your colors.xml file you can edit it.
When you click the colour on the left it opens color palette. And you can change it easily.
I have so specific problem. I'm applying theme for all textviews. It appears on preview but when I run this app on phone I face with unstyled textview. The styles I set for other views are working. only textviewstyle is not working.
In this sample textview color is green in preview but when i run it on phone its black. Why?
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textViewStyle">#style/myTextViewStyle</item>
<item name="android:buttonStyle">#style/myButtonStyle</item>
<item name="android:editTextStyle">#style/myEditTextStyle</item>
<!-- Customize your theme here. -->
</style>
<style name="myTextViewStyle" parent="#android:style/Widget.TextView">
<item name="android:textColor">#00ff00</item>
<item name="android:textSize">24sp</item>
</style>
<style name="myButtonStyle" parent="#android:style/Widget.Button">
<item name="android:textColor">#00ff00</item>
<item name="android:textSize">24sp</item>
</style>
<style name="myEditTextStyle" parent="#android:style/Widget.EditText">
<item name="android:textColor">#00ff00</item>
<item name="android:textSize">24sp</item>
</style>
activity_main.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="edit text" />
"textViewStyle" is not defined in v22.1.1. You can find more detail from the link:
https://code.google.com/p/android/issues/detail?id=170476
Is there a way to change a style based on what theme is is set on an activity? for example, suppose my activity has a textview with a style set to TextViewStyle
<TextView
style="#style/TextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text"/>
This is my TextViewStyle
<style name="TextViewStyle" parent="android:style/Widget.TextView">
<item name="android:textStyle">bold</item>
<item name="android:textSize">8sp</item>
<item name="android:textColor">#android:color/holo_blue_dark</item>
</style>
In my Styles.xml I have two themes
<style name="AppThemeLight" parent="android:Theme.Material.Light">
</style>
<style name="AppThemeDark" parent="android:Theme.Material">
</style>
Now, when I have my theme set to Dark, I want this theme to override some values I set in my TextViewStyle. How could I do this? I tried the following, but It does not work
<!-- Override TextViewStyle style for AppThemeLight-->
<style name ="AppThemeLight.TextBlockStyle" parent="#style/TextBlockStyle">
<item name="android:textColor">#color/my_purple</item>
<item name="android:textSize">20sp</item>
</style>
<!-- Override TextViewStyle style for AppThemeDark-->
<style name ="AppThemeDark.TextBlockStyle" parent="#style/TextBlockStyle">
<item name="android:textColor">#color/my_green</item>
<item name="android:textSize">40sp</item>
</style>
Ok here is how I achieved this, 1 hour after posting the question! Might be helpful for someone else!
First in attrs.xml I defined the following :
<attr name="textBlockStyle" format="reference"/>
Then in the style.xml
<style name="TextBlockStyle" parent="android:style/Widget.TextView">
<item name="android:textStyle">bold</item>
<item name="android:textSize">8sp</item>
<item name="android:textColor">#android:color/holo_blue_dark</item>
</style>
<style name="AppThemeLight" parent="android:Theme.Material.Light">
<item name="android:windowNoTitle">true</item>
<item name="textBlockStyle">#style/AppThemeLightTextBlockStyle</item>
</style>
<style name="AppThemeDark" parent="android:Theme.Material">
<item name="android:windowNoTitle">true</item>
<item name="textBlockStyle">#style/AppThemeDarkTextBlockStyle</item>
</style>
<!-- Override TextBlockStyle style for AppThemeLight -->
<style name ="AppThemeLightTextBlockStyle" parent="#style/TextBlockStyle">
<item name="android:textColor">#color/my_purple</item>
<item name="android:textSize">20sp</item>
</style>
<!-- Override MyButton style for AppThemeDark -->
<style name ="AppThemeDarkTextBlockStyle" parent="#style/TextBlockStyle">
<item name="android:textColor">#color/my_green</item>
<item name="android:textSize">40sp</item>
</style>
Notice how I use the attribute I defined to reference a style
<item name="textBlockStyle">#style/AppThemeLightTextBlockStyle</item>
Finally, on my element I set the style like this "style="?textBlockStyle" :
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?textBlockStyle"
android:text="...."
android:gravity="center" />
When I set my activity theme to Dark or Light, the correct textButtenStyle will be used
Add a textStyle element to your activity style and make it equal to the desired style, then when the activity is loaded all textViews inside will have the style specified inside its theme unless you override it in your textView
Try something like
<item name="android:textAppearance">#style/yourTextViewStyle</item>
Inside your activity style