Manos Rasidakis
Manos Rasidakis

Reputation: 3

Change background of whole app

I want to give the users of my app the option to change the background of the whole app . My solution is to create a button and in the onclicklistener, i would change the background of each activity separately with setBackgroundResource.

Is there a better way to do this?Or my way is sufficient?

Upvotes: 0

Views: 59

Answers (1)

Khemraj Sharma
Khemraj Sharma

Reputation: 58964

Put this style in res>values>styles.xml

<style name="bgThemeDark" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@drawable/bg1</item>
</style>    

and

<style name="bgThemeLight" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@drawable/bg2</item>
</style>    

Change @drawable/bg2 and @drawable/bg1 with your background resource.

Then make an BaseActivity in your app, extend all activity by BaseActivity.

Then write this in BaseActivity onCreate

boolean darkTheme = true;

public void onCreate(Bundle savedInstanceState) {
    setTheme(darkTheme ? R.style.bgThemeDark:R.style.bgThemeLight);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
}

You can also set this in manifest if you don't want change theme at run time.

<application
    android:theme="@style/CustomBackgroundTheme"

or

<activity
      android:name=".appClasses.activities.ActivityMain"
      android:theme="@style/CustomBackgroundTheme"
      >

Caution: You should not set any background of any activity layout parent nodes.

Upvotes: 1

Related Questions