I would like to know how to create a code TextView
and ImageView
and destroy it via code.
I would like to know how to create a code TextView
and ImageView
and destroy it via code.
To create a view in memory is simple, but most problems are when nothing appears on the screen, so make sure it is visible as in ImageView if you do not have an image will not appear anything, and if not add to any layout is totally impossible to appear. So in the case of TextView:
TextView textView = new TextView(this);
textView.setText("Alguma coisa");
//Suponhamos que temos uma layout com um id layoutVertical
LinearLayout linear = (LinearLayout)findViewById(R.id.layoutVertical);
linear.addView(textView);
In the case of imageView it would be something like the least part of which you have to add an image:
ImageView imagem = new ImageView(this);
imagem.setBackgroundResource(R.drawable.imagemqualquer);
In case of deleting what would have to do would remove the views of the layout they were in, in the case of the previous example to remove the textView we would have to do:
linear.removeView(textView);
Since the moment the TextView object is no longer used or called, the garbage Collector
clears it and no longer takes up memory. But even if you want you can also hide the view without deleting:
textView.setVisibility(View.GONE);