For example, I have an ImageView got by this:
public ImageView get_image_by_name(String name)
{
int image_id = getResources().getIdentifier(name , "drawable", getPackageName());
return (ImageView)findViewById(image_id);
}
Not sure it works at all.
How shall I add it to layout?
Assuming this function is placed somewhere within Activity, you can try this:
public ImageView get_image_by_name(String name)
{
int image_id = getResources().getIdentifier(name , "drawable", getPackageName());
ImageView iv = new ImageView(this);
ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(lp);
iv.setImageDrawable(getResources().getDrawable(image_id));
return iv;
}
You can also change LayoutParams to meet your requirements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With