Changing Imageview Source

Changing ImageView source

Changing ImageView source:

Using setBackgroundResource() method:

  myImgView.setBackgroundResource(R.drawable.monkey);

you are putting that monkey in the background.

I suggest the use of setImageResource() method:

  myImgView.setImageResource(R.drawable.monkey);

or with setImageDrawable() method:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));

*** With new android API 22 getResources().getDrawable() is now deprecated. This is an example how to use now:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey, getApplicationContext().getTheme()));

and how to validate for old API versions:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey, getApplicationContext().getTheme()));
} else {
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.monkey));
}

How to change ImageView source in android

whoamiwith.setImageResource(R.drawable.loginbtn);

Change Image of ImageView programmatically in Android

That happens because you're setting the src of the ImageView instead of the background.

Use this instead:

qImageView.setBackgroundResource(R.drawable.thumbs_down);

Here's a thread that talks about the differences between the two methods.

How to change imageview source from another class?

You answer does not help you to upate the ImageView in the ListView. You just make a new View (anotherView) which is not related to anything. To update your ImageView in the ListView, you should have some related info (ImageID or ImagePath etc) in your DataList. After the info is set/updated, just call adapter.notifyDataSetChanged() inside your Fragment.

In addition, you may modify your ListRequestAdapter to hide the ImageView if no data, like this:

public class ListRequestAdapter extends ArrayAdapter<ListRequestItem> {

public ListRequestAdapter(Context context, List<ListRequestItem> items){
super(context, R.layout.style_fragment_list_request, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.style_fragment_list_request, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.imgPhoto = (ImageView) convertView.findViewById(R.id.listPhoto);
viewHolder.tvTanggal = (TextView) convertView.findViewById(R.id.tvTanggalRequest);
viewHolder.tvTipe = (TextView) convertView.findViewById(R.id.tvTipeRequest);
viewHolder.tvStatus = (TextView) convertView.findViewById(R.id.tvStatus);
viewHolder.permitid = "";
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListRequestItem item = getItem(position);

// Setup ImageView accord to DataList,
// may be imageID from DataList.
if(imageID == null) viewHolder.imgPhoto.setVisibility(View.INVISIBLE);
else{
viewHolder.imgPhoto.setImageResource(item.imageID);
viewHolder.setVisibility(VIEW_VISIBLE);
}

viewHolder.tvTanggal.setText(item.tanggal);
viewHolder.tvTipe.setText(item.tipe);
viewHolder.tvStatus.setText(item.status);
viewHolder.permitid = item.permitid;
return convertView;
}

private static class ViewHolder {
ImageView imgPhoto;
TextView tvTanggal;
TextView tvTipe;
TextView tvStatus;
String permitid;
}
}

Hope this help!

Changing image source of an imageview in a different layout

Well remember that the class inflated with the layout will only find views in that layout, try to findById a view not in that layout will cause errors.
To resolve this, define a custom layout inflater, inflate the layout with the image , then used that view inflated and perform a findById on it.

How to change ImageView source from different XML?

You're inflating two separate views. Obviously what you do in one will have no impact on the other. You should set the popup view to your originally inflated view:

popupWindow = new PopupWindow(view1,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT,
true);

I need a way to change the image displayed by an ImageView in Android Studio (in Kotlin)

You need a reference to the ImageView (for example, using ViewBinding or findViewById), then you can use the method setImageResource, as stated in the documentation

Example:

// declare ImageView reference
var myImageView: ImageView

// in onCreate
myImageView = findViewById(R.id.myImageView)

// change image
myImageView.setImageResource(R.drawable.imageName)


Related Topics



Leave a reply



Submit