richard
richard

Reputation: 21

Android ImageView.setBackgroundResource() doesn't work

I'm trying to setBackgroundResource for an ImageView, but it doesn't work.

Xml:

<ImageView
    android:id="@+id/img_index"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginBottom="4dp"
    android:background="@drawable/homegray"
    android:gravity="center_horizontal|center_horizontal"
    android:paddingBottom="4dp"
/>

Activity code:

imgIndex = (ImageView) this.findViewById(R.id.img_index);
imgIndex.setBackgroundResource(R.drawable.homewhite);

How can I control my ImageView?

Upvotes: 0

Views: 4077

Answers (1)

Bishan
Bishan

Reputation: 15740

Try this.

imgIndex = (ImageView) findViewById(R.id.img_index);
imgIndex.setImageResource(R.drawable.homewhite);

or

imgIndex = (ImageView) findViewById(R.id.img_index);
imgIndex.setImageDrawable(getResources().getDrawable(R.drawable.homewhite));

Upvotes: 4

Related Questions