Search code examples
androidandroid-actionbarbackground-imageandroid-themetitlebar

android: set background color for the entire screen dynamically, or how to set actionbar to overlay layout, but not overlap content


My app uses the Theme.Wallpaper family, which means the current wallpaper is used as the app's background. This may cause readability issues (depending on the user's favorite wallpaper) so I want to allow the user to choose an image as a background.

The way I am trying to implement it is:

@Override
protected void onResume() {
    // Get wallpaper preferences to check if user selected a wallpaper and
    // display it
    SharedPreferences wallpaperPref = getSharedPreferences(WallpaperActivity.WALLPAPER_PREFERENCES, MODE_PRIVATE);
    int selectedWallpaper = wallpaperPref.getInt(WallpaperActivity.SELECTED_WALLPAPER, 0);
    if (selectedWallpaper != 0) {
        findViewById(R.id.pager).setBackgroundResource(selectedWallpaper);
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(selectedWallpaper));
    } else {
        findViewById(R.id.pager).setBackgroundColor(Color.TRANSPARENT);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }
    super.onResume();
}

The shared preference contains the resource id of the selected wallpaper.

My problem is the titlebar and actionbar do not respond to this. Is there a way to make them also receive the new background?


Solution

    1. Make the background of the title bar and action bar transparent.
    2. Set your Window ActionBar Overlay to true.
    3. Pad your main view by the height of the ActionBar so the ActionBar doesn't cover up any of the content.

    Your ActionBar will now sit over the background view, instead of above it. It will be transparent, so as to allow the background to show through, and it will not be covered up by the ActionBar.