Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LayoutInflater sometimes (randomly) inflates with wrong background color

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.

like image 791
hunyadym Avatar asked Aug 08 '13 12:08

hunyadym


1 Answers

try manually

v.setBackgroundColor(getResources().getColor(R.color.app_menu_item_colored_background));
like image 172
JITHIN GOPAL Avatar answered Oct 21 '22 11:10

JITHIN GOPAL