Orgil
Orgil

Reputation: 711

How to automatically create imageview in android

My android project connected to server.And i retrieve data from server if it's 5 then create 5 imageview in linearlayout in other words automatically create imageview, depends on retrieved data.

Upvotes: 1

Views: 1370

Answers (2)

Raj Patel
Raj Patel

Reputation: 191

For Linear Layout:

LinearLayout linearLayout= new LinearLayout(this);
                 linearLayout.setOrientation(LinearLayout.VERTICAL);
                 linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
for(int i=0;i<Number_Images;i++){
//ImageView Setup
ImageView i mageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
}
//make visible to program


setContentView(linearLayout);

Hope this Help You.

Upvotes: 0

osayilgan
osayilgan

Reputation: 5893

This code Loops over number of images and creates new ImageView for each time, and then it adds them to the Parent Linear Layout. You can also set the LayoutParams of ImageView dynamically as well.

LinearLayout layout = (LinearLayout)findViewById(R.id.parentLayout);
for(int i=0;i<number_of_images;i++) {
    ImageView image = new ImageView(context);
    layout.addView(image);
}

Upvotes: 2

Related Questions