Search code examples
javaandroidandroid-layoutandroid-linearlayoutandroid-relativelayout

Dynamic imageView. Specified child already has parent


I'm trying to add dynamic images.

This is error i'm facing

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I also add

  ((ViewGroup)imageView.getParent()).removeView(imageView);

but didn't solve my problem

    @Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == ConstantsCustomGallery.REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {

                ArrayList<Image> images = data.getParcelableArrayListExtra(ConstantsCustomGallery.INTENT_EXTRA_IMAGES);


                for (int i = 0; i < images.size(); i++) {
                    filePath = Uri.fromFile(new File(images.get(i).path));
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200, 200);
                    imageView.setLayoutParams(lp);
                    ((ViewGroup)imageView.getParent()).removeView(imageView);
                    Glide.with(MainActivity.this).load(filePath).override(200,200).crossFade().into(imageView);
                    linearLayout.addView(imageView);


                }
                  buttonConfirm.setVisibility(View.VISIBLE);

                progressDialog.setProgress(0);

            }
}

Here is XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#f4f7f9"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:weightSum="1">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Logout"
                android:textSize="22sp"
                android:textColor="#DF0713"
                android:id="@+id/logout_text"
                android:layout_toLeftOf="@+id/logout"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/logout"
         android:layout_alignParentEnd="true"
            android:background="@drawable/logout"/>
        </RelativeLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"

            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:hint="Image Title"
                android:inputType="textEmailAddress"
                android:textColor="@color/colorPrimaryDark"
                android:textColorHint="@color/colorPrimaryDark" />
        </android.support.design.widget.TextInputLayout>


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_marginTop="10dp" />


    </LinearLayout>





    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_marginBottom="17dp"
        android:layout_marginEnd="17dp"
        android:clickable="true"
        app:fabSize="normal"
android:layout_below="@+id/linearLayout"
        app:backgroundTint="#DF0713"
        app:srcCompat="@drawable/splash"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        app:borderWidth="0dp"
        app:elevation="0dp"
        />
    </RelativeLayout>
</ScrollView>

Solution

  • Issue is you are adding the same image view again and again which is not possible. you need to create new ImageVIew and add it wherever you want

    for (int i = 0; i < images.size(); i++) {
        filePath = Uri.fromFile(new File(images.get(i).path));
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200, 200);
        ImageView iv = new ImageView(context); // context mean YourActivity.this or context object
        iv.setLayoutParams(lp);
        //((ViewGroup)imageView.getParent()).removeView(imageView);
        Glide.with(MainActivity.this).load(filePath).override(200,200).crossFade().into(iv);
        linearLayout.addView(iv);
    }