Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Possible overdraw: Root element paints background "

While running Android Lint on my Project I came across this warning

Possible overdraw: Root element paints background @drawable/main with a theme that also paints a background

Where inferred theme is @android:style/Theme.NoTitleBar.Fullscreen

Can someone explain to me why is this coming and how to remove it ??

My xml:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/main" //***LINT warning***
        android:orientation="vertical"
        android:weightSum="3" >

The part of manifest where theme is defined

 <application
        android:icon="@drawable/ic_logo"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
like image 590
Sunny Kumar Aditya Avatar asked Aug 16 '12 19:08

Sunny Kumar Aditya


3 Answers

To optimize your apps performance (avoid overdraw), you can do the following:

  • declare a theme in res/values/styles.xml

    <style name="MyTheme" parent="android:Theme">
        <item name="android:background">@drawable/main</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>
    

  • change the Manifest:

    <application
        android:icon="@drawable/ic_logo"
        android:label="@string/app_name"
        android:theme="@style/MyTheme" >
    
  • remove the background declaration in "My xml"
like image 95
nvrandow Avatar answered Nov 04 '22 06:11

nvrandow


Update

Check out the comments or check out this link. As Marcin mentions, my solution here is not a good approach since it can cause artefacts. I've been using it to avoid overdraw for a long time without any problems but in general might be not a thumb of rule according to the Chet Haase comments on this technique.

Original answer

The best approach I've found is to set as null the default background and apply the background you need in each layout.

The reason is when you set a default background in a theme, or even different themes with different backgrounds as suggested above, this means the whole layout will be fill with that background. In most cases you don't need a background filling 100% of the screen because you have toolbars, headers, footers and other elements on top of that background that will cause overdraw.

To apply null background on a theme:

<style
    name="ThemeName" parent="ParentTheme">
    <item name="android:windowBackground">@null</item>
</style>

To check overdraw just activate the Show Overdraw option in the dev options on your emulator/device. Don't trust 100% lint warnings because there was some bugs in the tracker that I'm not sure are completely fixed.

More information: What is overdraw and why is an issue?

like image 18
Sotti Avatar answered Nov 04 '22 06:11

Sotti


The reason why you get this lint warning is that your activity and your linear layout both try to draw the background. Therefore the visible area is drawn twice.

How to debug this? Run sdk/tools/hierarchyviewer and inspect the view hierarchy to see which view has background that is not shown. (You need to have an android device running dev build rom)

What's running under the hood? Note that almost all android themes have background specified, which means if you ever want to create a "LinearLayout" that covers the entire screen and with a backround, you had better either set activity's windowBackground="@null" or remove the background setting in the linear layout.

like image 2
Ian Wong Avatar answered Nov 04 '22 06:11

Ian Wong