I'm trying to manually set the image-view position within my app, the code is currently working but all the facebook images display at the top right and side on top of each other.
I do not have access to any adaptors, this is within the Facebook SDK, can anyone spot any problems with the following code:
My XML:
DisplayPhotos.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DisplayPhotosLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
This is set by the following:
fbManager = new FBLoginManager(this, R.layout.displayphotos, "-------", permissions);
Then I use the following to display images (working but all in same position)
int Row = 0;
int RowCount = 0;
int imageWidth = (int)getWindowManager().getDefaultDisplay().getWidth() / 3;
for(int _i = 0; _i < _noOfPhotos; _i++)
{
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.no_image);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(imageWidth, imageWidth);
rl.setMargins(imageWidth * _i, imageWidth * Row, 0,0);
RowCount = RowCount + 1;
if(RowCount == 3){Row = Row + 1;RowCount = 0;}
UrlImageViewHelper.setUrlDrawable(imageView, _userGallery.get(_i).getPicture());
addContentView(imageView, rl);
System.out.println(imageWidth * _i + " top: " + imageWidth * Row);
_imageViewArray.add(imageView);
}
I think you need a table layout, http://developer.android.com/guide/topics/ui/layout/grid.html
i've found a trick to place an imageview at absolute position xy inside a relative layout. the following code place the CENTER of a imageview at x,y, even outside the relativelayout boundaries.
/*
* relativelayout allow out-of-boudaries only along the alignment side.
* if aligned to left then you can have a negative x and overlap the
* left side, if aligned to right you can overlap the right
*
* let's put the image CENTERED on x,y
*
* If we are on the left side of the container
* | (x<dx/2) |<---imgDx-->|
* | |____________|
* |
* | x
* |<----------------> |
* |<----------> x-imgDx/2 |
* |____________________________________|
*
* if we goes past the half of the container
* <---imgDx--> |
* | |____________| |
* | x |
* |<---------------------> |
* | <----->| dx-(x+imgDx/2)
* |____________________________________|
*/
public void setBitmapPosition(ImageView iv, int x, int y) {
String log = "";
RelativeLayout rl = (RelativeLayout) iv.getParent();
// get bitmap size
int imgDx = iv.getLayoutParams().width;
int imgDy = iv.getLayoutParams().height;
// get container size
int dx = rl.getWidth();
int dy = rl.getHeight();
log = log + " XY:" + new Integer(x).toString() + ","
+ new Integer(y).toString();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
imgDx, imgDy);
iv.setLayoutParams(lp);
log = log + " imgXY:" + new Integer(imgDx).toString() + ","
+ new Integer(imgDy).toString();
log = log + " winXY:" + new Integer(dx).toString() + ","
+ new Integer(dy).toString();
if (x <= (dx / 2)) {
// i'm on the left side of the view so let's align to left
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
RelativeLayout.TRUE);
lp.leftMargin = x - imgDx / 2;
log = log + " LEFT:" + new Integer(lp.leftMargin).toString();
} else {
// align to right. we are past the middle
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
RelativeLayout.TRUE);
lp.rightMargin = dx - (x + imgDx / 2);
log = log + " RIGHT:" + new Integer(lp.rightMargin).toString();
}
if (y <= (dy / 2)) {
// align to top
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
lp.topMargin = y - imgDy / 2;
log = log + " TOP:" + new Integer(lp.topMargin).toString();
} else {
// align to bottom
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
lp.bottomMargin = dy - (y + imgDy / 2);
log = log + " BOTTOM:"
+ new Integer(lp.bottomMargin).toString();
}
iv.setLayoutParams(lp);
Log.i("PARAM", log);
}