1

I am making a puzzle game for which I have to display 16 images (4 X 4) on the screen at the same time. I am trying to set the height and width of images but no value of hieght and width is changing the image size. Moreover only 4 images are appearing instead of 16 images. I am using the folloing code to display images:

   public void display()
    {
        LinearLayout llMain = new LinearLayout(this);
        for(int i=0;i<4;i++)
        {
            LinearLayout llRow = new LinearLayout(this);
            for(int j=i*4;j<tiles.length/4;j++)
            {
                ImageView iv = new ImageView(this);
                iv.setImageBitmap(tiles[j]);
                iv.setAdjustViewBounds(true);
                iv.setMaxHeight(tileHeight);
                iv.setMaxWidth(tileWidth);
                iv.setMinimumHeight(tileHeight);
                iv.setMinimumWidth(tileWidth);
                LayoutParams params =  new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                iv.setLayoutParams(params);
                llRow.addView(iv);
            }
            llMain.addView(llRow);
        }
        setContentView(llMain);
}

Can somebody please tell me that What am I doing wrong?

Thanks in advance

Maria Niaz
  • 107
  • 1
  • 12
  • I think you'll need to change one of the `LinearLayout`s to a vertical orientation. `llMain.setOrientation(LinearLayout.VERTICAL);` – dokkaebi Jan 05 '13 at 02:16
  • 1
    Next problem I see is your loop counters. When i==0 you go from j=0 to j=4; when i==1 you go from j=4 to j=4; when i==2 you go from j=8 to j=4... You want `for (int j=0; j<4; ++j)` and `iv.setImageBitmap(tiles[i*4 + j])`. – dokkaebi Jan 05 '13 at 02:42
  • So, did setminimumWidth work for you?Please share if you got any solution – Shruti May 31 '17 at 12:18

1 Answers1

0

Try using the layout_weight attribute of LinearLayout to divide the screen into the parts you need. The ratio of the values you set to the different parts makes the parts become bigger or smaller.

Abhishek Sabbarwal
  • 3,758
  • 1
  • 26
  • 41
  • Sorry but can you please elaborate? how can I use layout_weight attribute and instead of what? – Maria Niaz Jan 05 '13 at 00:16
  • I am assuming that your imageviews are inside a LinearLayout. So you can divide the parent layout (Linear Layout in this case) amongst its children (Imageviews in this case) as per desired proportions using the Layout_width property of the Linear Layout. This property will have to be implemented in the xml. Check http://stackoverflow.com/questions/3084185/androidlayout-weight-beginners-question http://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean – Abhishek Sabbarwal Jan 05 '13 at 00:20
  • Thanks, but there is still a problem that I have to do it dynamically. The number of images may vary according to the level selection. If i do it in xml then I cannot change it later – Maria Niaz Jan 05 '13 at 00:26
  • You could get the reference of the parent layout (in this case Linear Layout) by LinearLayout l = ((LinearLayout)(findViewById(R.id.LinearLayout01))); and after this run a for loop to generate the specified number of image views at runtime and the last line in the for loop will add the image view to the linearlayout like this l.addView(i); (Here i is the image view which was dynamically created.) – Abhishek Sabbarwal Jan 05 '13 at 00:33
  • So in this case you will only have to have the LinearLayout defined in the xml and you can set the layout_weight property for the imageviews inside your for loop. – Abhishek Sabbarwal Jan 05 '13 at 00:36
  • Check http://stackoverflow.com/questions/4641072/how-to-set-layout-weight-attribute-dynamically-from-code for how to set the weight. – Abhishek Sabbarwal Jan 05 '13 at 00:54
  • sorry to interject here, but where is display() being called? –  Jan 05 '13 at 02:07
  • from another function shuffleArray().. Has it got something to do with this problem? – Maria Niaz Jan 05 '13 at 02:20