Create Imageviews Dynamically Inside A Loop
I have written this code that loads an image to in ImageView widget: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContent
Solution 1:
you can modify the layout , image resource and no of images (may be dynamic as well) according to your requirement...
LinearLayoutlayout= (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageViewimage=newImageView(this);
image.setLayoutParams(newandroid.view.ViewGroup.LayoutParams(80,60));
image.setMaxHeight(20);
image.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
Solution 2:
You can use this code to create ImageViews
ImageViewimage=newImageView(this);
LinearLayout.LayoutParamsvp=newLinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setMaxHeight(50);
image.setMaxWidth(50);
// other image settings
image.setImageDrawable(drawable);
theLayout.addView(image);
where theLayout is the layout you want to add your image views to.
For more customization check out the dev page, where all the possible options are listed.
Solution 3:
If your requirement is show in List or Gridview then you should go for Lazy Loading List or grid.
Please go through the below link.
Solution 4:
Try this
rootLayout = (LinearLayout) view1.findViewById(R.id.linearLayout1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_VERTICAL;
params.setMargins(0, 0, 60, 0);
for(int x=0;x<2;x++) {
ImageView image = new ImageView(getActivity());
image.setBackgroundResource(R.drawable.ic_swimming);
rootLayout.addView(image);
}
Post a Comment for "Create Imageviews Dynamically Inside A Loop"