0

Add View dynamically in LinearLayout and after adding View change any View background on the click button.

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
ImageView img1 = new ImageView(this);
img1.setLayoutParams(layoutParams);
img1.setImageBitmap(icon);
llLayout.addView(img1);

ImageView img2 = new ImageView(this);
img2.setLayoutParams(layoutParams);
img2.setImageBitmap(icon);
llLayout.addView(img2);

ImageView img3 = new ImageView(this);
img3.setLayoutParams(layoutParams);
img3.setImageBitmap(icon);
llLayout.addView(img3);   

On button, click change all imageview background or particular ImageView.

Note : llLayout is my linear layout this layout adding in XML

Abhi S
  • 250
  • 2
  • 18
  • Does this answer your question? [Adding content to a linear layout dynamically?](https://stackoverflow.com/questions/6661261/adding-content-to-a-linear-layout-dynamically) – crazo7924 Mar 01 '22 at 04:55
  • No, I know how to add view in layout but I don't know how to edit all view or particular view, @crazo7924 – Abhi S Mar 01 '22 at 04:59
  • You can always declare the child view variables in your activity/fragment class scope and call their functions in onClick of the button's view.listener object... – crazo7924 Mar 01 '22 at 05:04

2 Answers2

0

when you are adding imageView into linear layout, at that time you are setImageBitmap to imageView.

if you want to reset Image to Imageview, you should use img1.setImageResource

    btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                   //use anyone as your requirement
                    img1.setBackgroundResource(); // for set background resource from drawable folder(for src)
                    img1.setBackground();// for set background(Drawable)(for background)
                    img1.setBackgroundColor(); //for set background color(for background)
                    
                }
            });
EAS
  • 366
  • 2
  • 18
0

I am not sure if I understand the question correctly, but If you mean change specific image background with onClick event on a button .. If you have the image reference change it .. but if you mean you are adding the image view dynamically like inside for loop and you don't have the reference you can create Arraylist and add the added images into it then loop on this arraylist to change all the images background or filter the specific image you want to change