Reputation: 6228
I am trying to set a new image for every ImageView in my layout by following code. It does not generate any error. But pictures of the ImageViews does not change.
RelativeLayout rl = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_desert, null);
int count = rl.getChildCount();
Log.d("COUNT", "***************************** Child count = " + count);
for (int i = 0; i < count ; ++i){
ImageView im = (ImageView)rl.getChildAt(i);
im.setImageDrawable(getResources().getDrawable(R.drawable.kunefem));
}
Here is my xml file content: activity_desert.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Desserts"
tools:ignore="MergeRootFrame" >
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:id="@+id/image0"
android:src="@drawable/dessert2"
android:layout_marginLeft="305dp"
android:layout_marginTop="65dp"
android:layout_alignParentLeft="true" />
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:id="@+id/image1"
android:layout_marginLeft="35dp"
android:src="@drawable/dessert2"
android:longClickable="true"
android:layout_alignTop="@+id/image0"
android:layout_toRightOf="@+id/image0" />
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:id="@+id/image2"
android:layout_marginTop="47dp"
android:src="@drawable/dessert2"
android:layout_below="@+id/image0"
android:layout_alignLeft="@+id/image0" />
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:id="@+id/image3"
android:layout_marginLeft="46dp"
android:src="@drawable/dessert2"
android:layout_alignTop="@+id/image1"
android:layout_toRightOf="@+id/image1"
android:layout_alignParentStart="false" />
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:id="@+id/image4"
android:src="@drawable/dessert2"
android:layout_alignTop="@+id/image2"
android:layout_alignLeft="@+id/image1" />
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:id="@+id/image5"
android:src="@drawable/dessert2"
android:layout_alignTop="@+id/image4"
android:layout_alignLeft="@+id/image3" />
I am frustrated! Please help!
Upvotes: 0
Views: 402
Reputation: 424
Try the following code:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.container);
Then try accessing the child views of that object fetched.
EDIT: Apparently this worked, I'm glad this answered your question!
I wanted to add a little bit more of an explanation of why this worked. When it comes to actually grabbing the View objects from any layout.xml file, you have to use the findViewById method. A LayoutInflater instantiates a layout XML file into its corresponding view objects. Glad this helped you out!
Upvotes: 2