Setting Android Theme background color
Okay turned out that I made a really silly mistake. The device I am using for testing is running Android 4.0.4, API level 15.
The styles.xml file that I was editing is in the default values folder. I edited the styles.xml in values-v14 folder and it works all fine now.
Apply theme to view's background color
Here is how I handle such cases:
Create an attribute in attrs.xml (in values folder, if there isn't one, create it yourself) as:
<resources>
<attr name="myBackgroundColor" format="color" />
</resources>
There are 2 themes.xml if you are implementing the dark mode with the help of day-night theme. So in the themes.xml file which is located in values folder:
<item name="myBackgroundColor">#FFFFFF</item>
In the other themes.xml file which is located at values-night folder:
<item name="myBackgroundColor">#000000</item>
When you want the view to change it's background color accordingly with the phone theme:
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?myBackgroundColor"/>
That's it, android will handle the rest and pick the corresponding color you provided from night themes xml if the dark mode is enabled by the user.
how to change background color by changing the theme in android?
Sample layout
<FrameLayout
style="@style/colorAccentStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content/>
styles.xml
<resources>
<style name="colorAccentStyle">
<item name="android:background">?colorAccent</item>
</style>
Simpler solution
<FrameLayout
android:background="?colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
How to set an ImageButton background color to a specific color of a theme that could be adapted to the night mode?
According to Material Design guidelines (see here),
- In
styles.xml
, inherit from aDayNight
theme.
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
...
...
</style>
- Create a new
colors.xml
file under thevalues-night
directory.
- In
values\colors.xml
,
<color name="primary_color">{your-light-theme-color-here}</color>
In values-night\colors.xml
<color name="primary_color">{your-dark-theme-color-here}</color>
- In
styles.xml
,
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">@color/primary_color</item>
...
</style>
- Set this theme in your
AndroidManifest.xml
<application
...
android:theme="@style/Theme.MyApp">
</application>
I feel this is the most guideline-appropriate and efficient way.
Android - how to set background color of all screens?
A quick and easy way to make sure every activity has the same background color, is to create a theme for your activities to use. That theme would specify the android:windowBackground.
First define the color in values/colors.xml
<resources>
<color name="background">#FF0000 </color>
</resources>
Create a themes.xml file in res/values that references that color:
<resources>
<style name="MyTheme" parent="@android:style/Theme.Light">
<item name="android:windowBackground">@color/background</item>
</style>
</resources>
... and then in your AndroidManifest.xml specify this as the theme for your activities to use.
<activity
android:name=".MyActivity"
android:theme="@style/MyTheme" />
How do you change android app theme colors with default phone settings?
Please checkout this code lab Add Light/Dark Theme
Update the dark version of your theme
Open themes.xml (night) (app > res > values > themes > themes.xml (night))
Note: This themes.xml file is different from the previous themes.xml file.
This file contains the dark theme version of the theme.
The resources in this file will be used when Dark theme on the device is on.
When you're done, your themes.xml (night) file should look like this:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Application theme for dark theme. -->
<style name="Theme.TipTime" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/green_light</item>
<item name="colorPrimaryVariant">@color/green</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/blue_light</item>
<item name="colorSecondaryVariant">@color/blue_light</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>
</resources>
your theme structure should look like this
Android Change branded launch background colors when theme is dark
Just use a DayNight
theme (Theme.MaterialComponents.DayNight
or Theme.AppCompat.DayNight
) in your app.
Then use the -night
qualifier on your resource folders as drawable-night
and values-night
.
It means use the same name for your drawable in the branded launch for dark and light theme but use thedrawable-night
and drawable
folder.
You can do the same for the colors. Instead of using @color/splashColorDark
and @color/splashColorLight
use a single name splashColor
and put it in the values-night\colors.xml
and values\colors.xml
Note. Check the official documentation:
Launch screens
If your app has a custom launch screen, it may need to be modified so that it reflects the selected theme.
Remove any hardcoded colors, for example any background colors pointing may be white. Use the
?android:attr/colorBackground
theme attribute instead.Note that dark-themed
android:windowBackground
drawables only work on Android Q.
Android change Dialog background color from app settings via styles
I have read that on a newer devices the attributes with android: prefix should be used or to use AppCompat theme as a parent to get rid of confusing with this prefix.
Issue regarding background color of App title and button in Android Studio
Simple and Easy Answer:-
Do This:-
- For Changing Background Color Of Action Bar
<item name="colorPrimary">//Color of your choice</item>
- For Changing Color of Button
<Button android:backgroundTint="#32a852"/>
But, I would prefer you to use Toolbar instead of Action Bar.
Related Topics
Android Fragmenttransaction Custom Animation (Unknown Animator Name: Translate)
How to Change the Generated Filename for App Bundles with Gradle
How to Get Chrome Browsing History/Bookmarks in Our Android App
Android:Textallcaps="False" Not Working for Tablayout Design Support
Nullpointerexception from Google Maps
How to Detect If Android Device Is Paired with Android Wear Watch
Firebase Dynamic Link Not Opening the App
Issues with Android Tabhost Example
Viewing Excel Files in My Android App
Get the Co-Ordinates of a Touch Event on Android
Changing the Screen Brightness System Setting Android
Cannot Create an Instance of Class Viewmodel
Creating a Product Sdk: How to Add a Native Lib (.So) and a Jar with the Sdk I am Creating
Android:Record Sound in Mp3 Format
How to Check If Music Is Playing by Using a Broadcast Receiver