I am generating a string from database dynamically which has the same name of image in drawable
folder.
Now I want to set that value for ImageView
All the answers posted do not apply today. For example, getDrawable() is deprecated. Here is an updated answer, cheers!
ContextCompat.getDrawable(mContext, drawable)
From documented method
public static final android.graphics.drawable.Drawable getDrawable(@NotNull android.content.Context context,
@android.support.annotation.DrawableRes int id
Construct a POJO.java class and create "constructor, getter & setter methods"
class POJO{
public POJO(Drawable proImagePath) {
setProductImagePath(proImagePath);
}
public Drawable getProductImagePath() {
return productImagePath;
}
public void setProductImagePath(Drawable productImagePath) {
this.productImagePath = productImagePath;
}
}
Then setup the adapters through image drawable resources to CustomAdapter.java
class CustomAdapter extends ArrayAdapter<POJO>{
private ArrayList<POJO> cartList = new ArrayList<POJO>();
public MyCartAdapter(Context context, int resource) {
super(context, resource);
}
public MyCartAdapter(Context context, ArrayList<POJO> cartList) {
super(context, 0, cartList);
this.context = context;
this.cartList = cartList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
*Here you can setup your layout and references.
**/
ImageView productImage = (ImageView) rootView.findViewById(R.id.cart_pro_image);
productImage.setImageDrawable(POJO.getProductImagePath());
}
}
Then pass the references through ActivityClass.java
public class MyCartActivity extends AppCompatActivity{
POJO pojo;
CustomAdapter customAdapter;
ArrayList<POJO> cartList = new ArrayList<POJO>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
customAdapter = new CustomAdapter(this, cartList);
pojo = new POJO(getResources().getDrawable(R.drawable.help_green));
}
}
This works, at least in Android API 15
ImageView = imgv;
Resources res = getResources(); // need this to fetch the drawable
Drawable draw = res.getDrawable( R.drawable.image_name_in_drawable );
imgv.setImageDrawable(draw);
You could use setImageResource(), but the documentation specifies that "does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup ... consider using setImageDrawable() or setImageBitmap()." as stated by chetto
The 'R' file can not be generated at the run time of the app. You may use some other alternatives such as using if-else
or switch-case
I had the same problem as you and I did the following to solve it:
**IMAGEVIEW**.setImageResource(getActivity()
.getResources()
.getIdentifier("**IMG**", "drawable", getActivity()
.getPackageName()));
a piece of my project, everything works! )
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final ModelSystemTraining modelSystemTraining = items.get(position);
int icon = context.getResources().getIdentifier(String.valueOf(modelSystemTraining.getItemIcon()), "drawable", context.getPackageName());
final FragmentViewHolderSystem fragmentViewHolderSystem = (FragmentViewHolderSystem) holder;
final View itemView = fragmentViewHolderSystem.itemView;
// Set Icon
fragmentViewHolderSystem.trainingIconImage.setImageResource(icon);
// Set Title
fragmentViewHolderSystem.title.setText(modelSystemTraining.getItemTitle());
// Set Desc
fragmentViewHolderSystem.description.setText(modelSystemTraining.getItemDescription());