Home > Net >  Put an image as a background in every part of the app
Put an image as a background in every part of the app

Time:06-24

In a nutshell I should put this image as a background in every part of the app which is composed of many fragments and activities, For now, each xml file has its own background. I tried to look around but found nothing. How can I do? Thanks in advance

Light theme

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.ChatApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/toolbar</item>
        <item name="colorPrimaryDark">@color/toolbarDark</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/toolbarDark</item>
        <item name="colorSecondaryVariant">@color/teel_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">@color/toolbarDark</item>
        <!-- Customize your theme here. -->

        <item name="android:windowBackground">@drawable/main_background</item>
    </style>

    <style name="MenuStyle" parent="Theme.MaterialComponents.DayNight">
        <item name="android:windowDisablePreview">true</item>
        <item name="overlapAnchor">false</item>
        <item name="android:dropDownVerticalOffset">5.0dp</item>
    </style>

    <style name="TabStyle" parent="Widget.MaterialComponents.TabLayout">
        <item name="tabIndicatorColor">@color/toolbar</item>
        <item name="tabTextColor">@color/black</item>
        <item name="tabSelectedTextColor">@color/toolbar</item>
    </style>

</resources>

night theme

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Application theme for dark theme. -->
    <style name="Theme.ChatApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/toolbar</item>
        <item name="colorPrimaryDark">@color/toolbarDark</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/toolbarDark</item>
        <item name="colorSecondaryVariant">@color/toolbarDark</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">@color/toolbarDark</item>
        <!-- Customize your theme here. -->
        <item name="android:windowBackground">@drawable/main_background</item>
    </style>

    <style name="MenuStyle" parent="Theme.MaterialComponents.DayNight">
        <item name="android:background">@android:color/white</item>
    </style>

    <style name="TabStyle" parent="Widget.MaterialComponents.TabLayout">
        <item name="tabIndicatorColor">@color/toolbarDark</item>
        <item name="tabTextColor">@color/white</item>
        <item name="tabSelectedTextColor">@color/toolbarDark</item>
    </style>

</resources>

CodePudding user response:

You can try this. Add android:windowBackground tag in the parent theme. Like below:

<style name="Theme.ProjectXYZ" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">

        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryVariant">@color/white</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/black</item>
        <!--        <item name="colorSecondary">@android:color/transparent</item>-->
        <item name="colorSecondaryVariant">@color/black</item>
        <item name="colorOnSecondary">@color/white</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>

        **<item name="android:windowBackground">@drawable/sprinkles</item>**

    </style>

The final tag here is refering to the image in the drawable folder. This is make the backgrounds of all the frags activity the same

CodePudding user response:

Copy *.png image and paste to drawable folder.
Create a themes.xml file in res that references that image:

<resources>
 <style name="MyTheme" parent="@android:style/Theme.Light"> 
  <item name="android:windowBackground">@drawable/main_background</item>
 </style>
</resources>

and in your AndroidManifest.xml specify this as the theme for your activities to use.

 <activity
        android:name=".MyActivity"
        android:theme="@style/MyTheme" />
  • Related