Overdraw issue in fragment layout with both background color and theme existing

1k views Asked by At

Hi I am a newbie to Android Programming using AS. This should be a common/easy problem but I have not found a solution so far.

I was creating left_ and right_fragments tags in activity_main.xml for tablets, in which I wanted to have a background color in right_fragment(temporarily set to #ffffff and keep left_fragment as default). Then the Android Lint warned that I got an Overdraw: Painting regions more than once on the right_fragment:

I tried to create a new theme:

<style name="NoBgTheme" parent="android:Theme.Material.Light.DarkActionBar">
    <item name="android:windowBackground">@null</item>
</style>

and added android:theme="@style/NoBgTheme" to right_fragment.xml, but it did not work.

Screenshot of this app w/ Debug GPU Overdraw enabled.

If I change android:theme= from "@style/AppTheme" to "@style/NoBgTheme" in AndroidManifest.xml, it worked, but all other layouts lost their backgrounds.

Any idea how to fix this OVERDRAW? Thx in advance.


activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/left_fragment"
        android:name="jerryc05.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/right_fragment"
        android:name="jerryc05.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

left_fragment.xml is so simple that we can just ignore it.

right_fragment.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:theme="@style/NoBgTheme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff">

    <TextView
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="This is right fragment"
        android:textSize="20sp"
        tools:ignore="HardcodedText" />

</LinearLayout>

AndroidManifest.xml remains unchanged.

MainActivity.java remains unchanged.

2

There are 2 answers

2
dazza5000 On

A little bit of overdraw will happen. You need to set the nobgtheme to your main activity if you want to fix the overdraw on your left fragment and right fragment display. If this only happens during tablet layouts you can check to see if this is a tablet layout being used and set the no background theme at runtime using methods described here: How to change current Theme at runtime in Android

0
jerryc05 On

Alright, I got an alternative solution.

I added android:theme="@style/NoBgTheme" after <activity android:name=".MainActivity" before >.

Then I manually specified android:background="#f0f0f0" and android:background="#ffffff" to left_ and right_fragment.xml respectively.

It worked so well!!!

But the Android Lint Warning still exists...(is this a bug or still my fault?)