-4

In my app, I select photos from gallery and insert them inside of LinearLayout programmatically, so I did this :

<LinearLayout
            android:orientation="horizontal"
            android:id="@+id/linearImages"
            android:layout_width="wrap_content"
            android:layout_height="150dp">
            <Button
                android:layout_gravity="center"
                android:id="@+id/add_btn"
                android:gravity="center"
                android:drawableLeft="@drawable/add_icon"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

And i'm set a tag to which photo that I'm putting :

public void AddNewImages(Context context,Bitmap bitmap){
    ImageView img = new ImageView(context);
    img.setScaleType(ImageView.ScaleType.FIT_XY);
    img.setImageBitmap(bitmap);
    img.setTag(tagCount);
    linearImages.addView(img);
    bitmapArray.add(bitmap);
    tagCount++;
}

And I want to remove a Image by tapping, as I said before, the Images are added programmatically, so I need something to remove images one by one without have a static Image position.

  • This link may be helpful http://stackoverflow.com/questions/3995215/add-and-remove-views-in-android-dynamically – Randroid Feb 09 '17 at 12:02

2 Answers2

4

I would suggest to use RecyclerView instaed of LinearLayout. The reason being you can easily get the position of the item clicked and can remove accordingly. If you want to go with your own solution, then I would suggest to add setOclickListener on each ImageView being added. In the listener after the click event get the imagView.getTag(), which is the position of the image view within LinearLayout. You can then remove the image view from the LinearLayout by using:

ll.removeViewAt(position);// to remove view from particular position

Or if you want to remove ImageView being clicked directly then:

ll.removeView(view)// to remove particular view
Anurag Singh
  • 6,140
  • 2
  • 31
  • 47
1

Update your code of AddNewImages It will work.

public void AddNewImages(Context context,Bitmap bitmap){
        ImageView img = new ImageView(context);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setImageBitmap(bitmap);
        img.setTag(tagCount);
        linearImages.addView(img);
        bitmapArray.add(bitmap);
        tagCount++;
        img.setOnClickListener(clickListner);
        View.OnClickListener clickListner=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                linearImages.removeView(v);
            }
        };
    }
Rahul Giradkar
  • 1,818
  • 1
  • 17
  • 28
  • So this `ImageView img= (ImageView) v;` would be for what? –  Feb 09 '17 at 12:21
  • It is not required . you can directly call linearImages.removeView(v). – Rahul Giradkar Feb 09 '17 at 12:24
  • It works, at the first, but if I try to add again, i got this Exception "The specified child already has a parent. You must call removeView() on the child's parent first." –  Feb 09 '17 at 12:40
  • It is not possible Your are creating a new Instance of ImageView and By default it has not parent so how you get such exception. – Rahul Giradkar Feb 09 '17 at 12:55