I’ve got a strange issue. I have a class that inherits from RelativeLayout. I inflate to an xml. The default background image of this layout is set in the xml and I try to change it at run time when it’s touched:
if(event.getAction() == MotionEvent.ACTION_DOWN) {
System.out.println("Action down");
this.setBackgroundResource(R.drawable.event_cell_selected);
}
else if(event.getAction() == MotionEvent.ACTION_CANCEL) {
System.out.println("Action down");
this.setBackgroundResource(R.drawable.event_cell);
}
The resources event_cell and event_cell_selected are both .png files. Also, I see my logs. I really can’t say what’s going on here.
[edit]
Thanks guys for your quick answers. No error in logcat, here is the xml I inflate:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/event_cell"
android:paddingBottom="@dimen/padding_small"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:paddingTop="@dimen/padding_large" >
<TextView
android:id="@+id/textViewMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="juil."
android:textColor="@color/event_header"
android:textSize="19dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewMonth"
android:layout_marginBottom="@dimen/margin_small"
android:text="TextView"
android:textColor="@color/event_header"
android:textSize="15dp" />
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textColor="@color/event_header"
android:textSize="17dp" />
<ImageView
android:id="@+id/imageViewSeparator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textViewDay"
android:layout_marginBottom="@dimen/margin_small"
android:src="@drawable/event_title_descr_separator" />
<TextView
android:id="@+id/textViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageViewSeparator"
android:ellipsize="end"
android:maxLines="2"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="12dp" />
And here is my root xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/navigationBarView" >
<LinearLayout
android:id="@+id/eventsLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/margin_small"
android:paddingRight="@dimen/margin_small" >
</LinearLayout>
</ScrollView>
The inflated RelativeLayout is added to the scrollView’s linearlayout
[edit]
Ok to solve this problem I finally end-up setting my default background image programmatically (not through the xml). That does the trick. Android can really have weird behavior sometimes …
Show you layout xml and inflate code. I bet the problem is folowing: in you xml you are using RelativeLayout as a root. This means that then inflating you are adding relative layout defined in xml to the relative layout created in code (defined by you own class inherited from RelativeLayout). This means that background defined in xml is shown above backround you changing in code. Try using
<merge>in you layout root instead of<RelativeLayout>You can read about “merge” layout tag here
http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html