'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.
Solution 1:[1]
Here's how I would do it:
- In my fragment's view xml, have a ViewGroup called
R.id.containerto hold your ImageViews. InonCreateView, save a reference to this container. - 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!
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | chessdork |
| Solution 2 |
