LayoutInflater sometimes (randomly) inflates with wrong background color
Asked Answered
E

3

9

I inflate a view with LayoutInflater. The background color of my inflated RelativeLayout is set in my xml file. I have experienced a strange issue on one of my devices: sometimes (randomly) the background color was an other (wrong) color from my colors.xml. Have anyone met with this problem before?

Details:

I have a ListView with a CursorAdapter. I inflate the list items with only one, static item (so I think it's not a recycling issue), using this code:

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
    View v = vi.inflate(R.layout.bookmark_item, null) ;
    //bindView(v, context, cursor) ;
    Log.wtf("newView", "View color: " + Integer.toString(((ColorDrawable) (((RelativeLayout)v.findViewById(R.id.bookmark_row)).getBackground())).getColor())) ;
    return v;
}

My layout/bookmark_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/bookmark_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/app_menu_item_background"
        tools:ignore="UselessParent" >

        <TextView 
            android:id="@+id/bookmark_item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
        />
    </RelativeLayout>

</RelativeLayout>

The inner RelativeLayout's background color is set to @color/app_menu_item_background. This color is in my R.java:

public static final int app_menu_item_background=0x7f0a0036; 

I also have a color named @color/app_menu_item_colored_background, what I use elsewhere in my code, it has nothing common with my bookmark list and the adapter. It also has a different resource id in R.java:

public static final int app_menu_item_colored_background=0x7f0a0038;

And both of them are different colors:

<color name="app_menu_item_background">#517409</color>
<color name="app_menu_item_colored_background">#f6efde</color>

Then, when running my app, sometimes (not always), my View is using the wrong app_menu_item_colored_background background color. I logged the inflated view colors (see code above), and it even differs sometimes:

"newView" - "View color: -11439095"
"newView" - "View color: -593954"

Note, that the first color is the #517409, the second is #f6efde.

Strangely, I could reproduce the error only on one device, a Samsung Galaxy S3 mini, about 2-3 times from every 10 tries.

Equilibrium answered 8/8, 2013 at 12:45 Comment(10)
I've been having the same problems, so i did the dirty solution of setting colors manually, but after some close analysis i believe its a memory leakage/mismanagement issue that's messing with the layouts. It's very weird! Also it could be something about the RelativeLayouts. Post a reason/solution if you find one.Urology
https://mcmap.net/q/1320721/-strange-behavior-of-inflated-buttons-when-changing-one-39-s-color This looks like a solution!Urology
You are trying to print hex values as string using Integer.tostring() so it will always return something like thatSastruga
Try removing tools:ignore="UselessParent"Sastruga
@Arju 03:53: Yes, I know that that's why it's printed like this (I mentioned that in the next sentence) - but it can't be the cause of the problem.Equilibrium
since the parent is wrap_content why can't you just set the android:background="@color/app_menu_item_background" to TextView directly,. Then try a bitSastruga
@Arju 03:56: I think that line is only to suppress the lint error message (I need the two layouts)Equilibrium
@Arju 08:14: I made a workaround (setting all colors from code - it was part of an urgent project in August), so I don't use this code now. Yes, maybe other workarounds would work, but the real interesting problem is, that why is it inflating a color, which I use nowhere in that part of the code?Equilibrium
@Equilibrium hey , i am also facing the same problem, i am using a xml drawable to inflate as a background of various layouts but unfortunately sometimes the solid color is not what i have given in the drawable file, did you find any solution or reason of such behaviour ?Rooseveltroost
No, I wasn't looking after it; I made a workaround that time, and the problem didn't appear in another projects.Equilibrium
T
1

try manually

v.setBackgroundColor(getResources().getColor(R.color.app_menu_item_colored_background));
Tanto answered 12/11, 2015 at 9:14 Comment(1)
As I noted in the comments almost 2 years ago, I used this workaround to solve the problem, but it doesn't explain, why this really strange problem occured.Equilibrium
P
0

I had this for my theme's windowBackground for an S2 running 4.1. As described in this answer, I used a solid state drawable in place of a colour as a workaround. I have a hunch this would work for view backgrounds as well.

I don't know the reason for this behaviour though.

Palm answered 14/6, 2016 at 22:57 Comment(0)
B
0

I heard it before. In your adapter, where you set your back ground, if you are not setting background this will occurs. So you should set your background in every getView called. If you are not setting this background and you want to save its state so set as null that view.

Boresome answered 14/6, 2016 at 23:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.