Android programmatically created ImageView not showing
I am trying to create an array ImageViews
programmatically. Here is my code (i and j for counters)
imageViews = new ArrayList<ImageView>();
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(3*i+j != 8) {
ImageView subImage = new ImageView(this);
subImage.setImageBitmap(pieces.get(3 * i + j));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(100, 100);
subImage.setLayoutParams(lp);
subImage.setX(i * 100f);
subImage.setY(j * 100f);
subImage.setVisibility(View.VISIBLE);
imageViews.add(subImage);
}
}
}
pieces is an arrayist of bitmaps. Anyway, none of the image views are displayed. I'm new to all of this, so I'm sure I'm making all sorts of mistakes. Thanks in advance!
source to share
You need to create layout in XML file
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout"
android:orientation="vertical">
</LinearLayout>
and in java file
LinearLayout layout=(LinearLayout)view.findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
imageViews = new ArrayList<ImageView>();
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(3*i+j != 8) {
ImageView subImage = new ImageView(this);
subImage.setImageBitmap(pieces.get(3 * i + j));
subImage.setVisibility(View.VISIBLE);
subImage.setLayoutParams(params);
imageViews.add(subImage);
layout.addView(subImage);
}
}
}
NOTE : if you have more images that are not set on screen then first create a scroll and insert a linear layout
source to share
you've created all the images, but you need a layout to display those images. let's say LinearLayout, so first create the layout in your xml file,
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myLinearLayout"
android:orientation="vertical">
</LinearLayout>
Now add images inside this layout.
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
imageViews = new ArrayList<ImageView>();
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(3*i+j != 8) {
ImageView subImage = new ImageView(this);
subImage.setImageBitmap(pieces.get(3 * i + j));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(100, 100);
subImage.setLayoutParams(lp);
subImage.setX(i * 100f);
subImage.setY(j * 100f);
subImage.setVisibility(View.VISIBLE);
imageViews.add(subImage);
}
}
}
for(int i=0; i< imageViews.size(); i++){ // iterating through the arraylist and adding the images to the linearlayout
mylinearLayout.addView(imageViews.get(0));
}
source to share
you have an array of image objects. just rename this array list and add to main parent layout
Suppose your main layout is RelativeLayout
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainlayout">
</LinearLayout>
store this object in java code
RelativeLayout rl=(RelativeLayout)findViewById(R.id.mainlayout);
//now iterate the arraylist of imageviews
for(ImageView iv:imageViews)
{
rl.addView(iv);
}
source to share