0

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.

jerryc05
  • 454
  • 1
  • 4
  • 16

2 Answers2

0

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

dazza5000
  • 7,075
  • 9
  • 44
  • 89
  • Thx for your answer. But setting the theme at runtime did not solve the problem. It worked just as changing `android:theme=` from `"@style/AppTheme"` to `"@style/NoBgTheme"` in `AndroidManifest.xml`, which resulted in the `left_fragment` to lose its background showing some wired patterns instead, although the `right_fragment` worked correctly in this way. You see, the MainActivity shows two fragments at the same time, and only the right one has overdraw problem not the entire activity. – jerryc05 Feb 12 '18 at 11:17
  • Nevermind bro I got an alternative solution posted below. btw do you have a better solution that does not need to change the theme of the activity(only change the right_fragment)? – jerryc05 Feb 12 '18 at 11:38
0

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?)

jerryc05
  • 454
  • 1
  • 4
  • 16