Reputation: 453
How to change resource of an image?
final Switch o = (Switch)findViewById(R.id.obamaswitch);
if (o.isChecked()){
o.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
ImageView p = (ImageView)findViewById(R.id.obamahere);
p.???;
}
});
Upvotes: 1
Views: 2950
Reputation: 2860
If you want to change image Resource onChecked switch then you can do this
ImageView image = (ImageView)findViewById(R.id.obamahere);
final Switch o = (Switch)findViewById(R.id.obamaswitch);
if (o.isChecked()){
image.setImageResource(R.drawble.icon);
}
and if you want to setImageResource when click on swith then do this
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
image.setImageResource(R.drawble.icon);
}else{
do your work...
}
}
});
Upvotes: 1
Reputation: 3843
Did you mean setting image programmatically, if yes then you can use setImageResource() property of imageview.
In your case the code will become as
p.setImageResource(R.drawable.<yourimagename>)
In case you wish to set image from file the you can do as follows
File imgFile = new File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
p.setImageBitmap(myBitmap);
}
Hope this will meet your needs
Upvotes: 0