Ruchir Baronia
Ruchir Baronia

Reputation: 7571

How to add imageview to fragment?

There are tons of questions like this, but they all address adding a view in onCreateView() before returning the root layout. I want to add a view in the middle of code execution, in onClick()


Note this is a fragment, which is why I can't update the UI without onCreateView():

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:

            //RelativeLayout Setup
            RelativeLayout relativeLayout = new RelativeLayout(getActivity());

            relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT));

            //ImageView Setup
            ImageView imageView = new ImageView(getActivity());

            //setting image resource
            imageView.setImageResource(R.drawable.lit);

            //setting image position
            imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.BELOW, R.id.button);

            imageView.setLayoutParams(params);
            //adding view to layout
            relativeLayout.addView(imageView);


            break;
    }
}

Here I get an instance of the layout and modify it. However, I cannot apply this modified fragment layout back into the application UI. How can I update the app interface after fragment UI modification?

Thanks for your time.

Upvotes: 1

Views: 8873

Answers (2)

Alex Hong
Alex Hong

Reputation: 281

You have just created a view, but haven't add to fragment's layout yet.

Method onCreateView() will return the container of this fragment, save this instance (ex: ViewGroup container)

And when you create a view, like your RelativeLayout in onClick(), add it to container and your UI will be update.

container.addView(relativeLayout);

Example:

public class MyFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.example_fragment, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();

        // i did this to see what see how button displays
        getView().postDelayed(new Runnable() {
            @Override
            public void run() {
                // I create a new button and add it to the fragment's layout.
                Button button = new Button(getActivity());
                ((LinearLayout)getView()).addView(button);
            }
        }, 2000);
    }
}

The layout example_fragment is simply a LinearLayout

Upvotes: 0

chessdork
chessdork

Reputation: 2028

Here's how I would do it:

  1. In my fragment's view xml, have a ViewGroup called R.id.container to hold your ImageViews. In onCreateView, save a reference to this container.
  2. Have a separate xml containing just the ImageView. This way, you don't have to do any programmatic layout.

Then in your onClick, or wherever you need to add the ImageView:

ImageView newView = LayoutInflater.from(getActivity).inflate(R.layout.image.xml);
mImageContainer.addView(newView);
// Ta da!

Upvotes: 0

Related Questions