android imageview set image from drawable programmatically code example

Example 1: how to set drawable name to imageview programmatically

String uri = "@drawable/myresource";  // where myresource (without the extension) is the file

int imageResource = getResources().getIdentifier(uri, null, getPackageName());

imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);

Example 2: how to create a new imageview in android java

ImageView move = new ImageView(this);

Tags:

Java Example