I want to create a dynamic number of ImageViews in Android. But to display these ImageViews I have to create them in main.xml (am I right?) Is there a way to display ImageViews without creating them in main.xml?
You can create them dynamically as follows:
ImageView image=new ImageView(this);
and to set the image in this dynamically created view, use:
image.setImageResource(R.drawable.yourimage);
Put the image my_image.jpg in res/drawable/my_image.jpg and use:
my_image.jpg
res/drawable/my_image.jpg
import android.app.Activity; import android.os.Bundle; import android.widget.ImageView; public class Main extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final ImageView imageView = new ImageView(this); imageView.setImageResource(R.drawable.my_image); setContentView(imageView); } }
Tested on Android 22 with the default template generated by android create project [...] .
android create project [...]
Source: https://habr.com/ru/post/1399656/More articles:Does the SML (Poly) CL-like REPL? - read-eval-print-loopPoint a long pointer to a function? - cLooping in Bash: syntax error: unexpected end of file - linuxBash Shell Script loop - linuxHow to install LLVM Clang on iPad? - clangForcibly delete the file, as in "rm -f", or cancel the file path from the directory - pythonDynamically invoke macros in Twig? - symfonyHttpUrlConnection inconsistent behavior on Android - how to debug? - androidDoes anyone know the difference between endl (cout) and cout << endl? - c ++How to measure the peak-to-noise ratio of images? - filterAll Articles