Onlayout method on custom layout extending RelativeLayout
14,400
I explain how to write custom layouts (and in particular a FlowLayout, which is what you want to do it seems like) in this presentation
Author by
Lovro Pandžić
Updated on June 04, 2022Comments
-
Lovro Pandžić almost 2 years
What is the proper way to override onLayout method in a custom layout extending the RelativeLayout? I'm trying to place all views in sort of a table. The idea is to fill one row with ImageViews until it's full and then continue in the new row.
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); int idOfViewToTheLeft = 1; RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(getContext(),); ImageView bookmark; for(int counter = 1; counter < getChildCount(); counter++) { bookmark = (ImageView) findViewById(counter); if(counter > 1) { if(this.getWidth() > (bookmark.getLeft() + bookmark.getWidth())) { params.addRule(RelativeLayout.RIGHT_OF, bookmark.getId() - 1); } else { params.addRule(RelativeLayout.BELOW, idOfViewToTheLeft); idOfViewToTheLeft = bookmark.getId(); } } bookmark.setScaleType(ScaleType.CENTER_CROP); } } }
-
Lovro Pandžić about 13 yearsI have another problem and it seems it's related to the relative layout. First fow of ImageViews is fine except of last one which gets cut off, those after that should be in next row don't appear there. I used lp.addRule(RelativeLayout.BELOW, idOfViewToTheLeft); but it doesn't seem to work. Am I doing something wrong, is it enough only to override onLayout method? @Romain Guy
-
rogerstone about 12 years@Romain Guy :Thanks a lot.Excellent tutorial.
-
joar about 11 yearsContent inaccessible. Parley's struggling with bad load times and crappy UI, Google Docs zipfile not loading fully. Might be an issue on my end, but I'd rather have the actual answer to the question in the post on StackOverflow.