0

I'm trying to dynamically add an Image to a dynamically created ImageView. The app crashes as soon as I select an Image. I tried it with a non-dynamiclly created view and it worked fine. I'm not sure where my problem is.

I've inflated the layout containing the ImageView, should I inflate the ImageView also? I'm not sure where my problem is. This is my main class.

public class MainActivity extends AppCompatActivity {

int clickCounterIndex = 0;
LinearLayout picsLayout;
LayoutInflater inflater;
View picItem;

Intent intentForPic;
int RESULT_LOAD_IMAGE = 1;
ImageView pic;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    intentForPic = new Intent(Intent.ACTION_GET_CONTENT);
    pic = (ImageView) findViewById(R.id.picImageView);

    picsLayout = (LinearLayout)findViewById(R.id.picsLayout);
    inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public void addStuff(View view) {

    intentForPic = new Intent(Intent.ACTION_GET_CONTENT);
    intentForPic.setType("image/*");
    startActivityForResult(intentForPic, RESULT_LOAD_IMAGE);

    picItem = inflater.inflate(R.layout.item_layout, picsLayout, false);
    picsLayout.addView(picItem, clickCounterIndex);
    clickCounterIndex++;

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
        Uri imageUri = data.getData();
        pic.setImageURI(imageUri);

    }
}
}

Main layout file.

<LinearLayout android:id="@+id/picsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="top"
xmlns:android="http://schemas.android.com/apk/res/android">

<Button
    android:text="click me!"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="addStuff"/>

</LinearLayout>

The dynamically added layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
tools:context="com.example.k0k0.thenextsnapchat.imageuploaddemo.MainActivity">

<ImageView
    android:id="@+id/picImageView"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:src="@android:drawable/ic_popup_disk_full"/>

<ProgressBar
    android:id="@+id/picUploadProgressBar"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:progress="100"/>

<ImageButton
    android:id="@+id/xImageButton"
    android:src="@android:drawable/btn_minus"
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="38dp"
    android:layout_marginBottom="38dp"/>

</LinearLayout>

1 Answer 1

1

Since your ImageView is part of you dynamically created view. So you have to hold a refrence to it like this:

picItem = inflater.inflate(R.layout.item_layout, picsLayout, false);

pic = (ImageView) picItem.findViewById(R.id.picImageView);

picsLayout.addView(picItem, clickCounterIndex);
clickCounterIndex++;
Sign up to request clarification or add additional context in comments.

3 Comments

This works but only the first time. When I try to add more items it doesn't display an Image anymore. Why do you think that is?
because you only have a single ImageView in your layout. If you dont know the amount of images there is you should go with a ListView or RecyclerView
I did try it with a <code>ListView</code> but it only worked when I'd set a fixed height of the <code>ListView</code>. How would you tackle that sort of problem? What do you think might've caused it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.