Reputation: 5971
I have an imageview that should be changed on click
public class Settings extends Activity implements OnClickListener
{
private ImageView im1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
im1 = (ImageView) findViewById( R.id.imageView1 );
im1.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (v == im1 )
{
Log.d("test", "hey!");
v.setBackgroundResource(R.drawable.img1);
}
}
}
when clicked the method runs and prints out "hey!" but the image won't change?
EDIT: forgot to mention that imageview
contains another image provided by xml layout file
Upvotes: 15
Views: 51426
Reputation: 91
Try This In API 25
imgSchedule.setImageDrawable(getResources().getDrawable(R.drawable.circle_image_selected));
Upvotes: 1
Reputation: 22945
UPDATE
Now, you can use like below,
imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.img1));
Upvotes: 2
Reputation: 1251
Its no convention that setimageResource() should be used. Both the APIs can be used.
Also, in your case, it just looks like the case of out of sync resources.
Upvotes: 0
Reputation: 333
ImageView i = (ImageView) findViewById( R.id.imageView1 );
i.setImageResource(R.id.logo);
or
i.setBackgroundResource(R.drawable.icon);
Upvotes: 4
Reputation: 22859
By convention, you should be using setImageResource(R.drawable.img1);
(or setImageDrawable(getResources().getDrawable(R.drawable.img1));
) instead of setBackgroundResource(R.drawable.img1);
.
Upvotes: 50