1. Download our Official Android App: Forums for Android!

Apps Custom Layout extending RelativeLayout

Discussion in 'Android Development' started by zekonja, Feb 14, 2011.

  1. zekonja

    zekonja Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Oct 6, 2010

    Oct 6, 2010
    5
    0
    5
    Hello, I've problem with extending this layout. I don't seem to be able to find any good tutorials on this. I've dynamic views that I need to place on screen in rows, after the row has been fulled (getWidth methods in if) it should place the next one below the one on the left in current row. Here's the code (I've only overloaded onLayout method):

    Code (Text):
    1. protected void onLayout(boolean changed, int l, int t, int r, int b) {
    2.         super.onLayout(changed, l, t, r, b);
    3.         LayoutParams lp;
    4.        
    5.         ImageView bookmark, previousBookmark;
    6.        
    7.         int idOfViewToTheLeft = 0; // Id of a View that is farthest to the left
    8.        
    9.         final int count = getChildCount();
    10.        
    11.         for(int i = 0; i < count; i++) {
    12.            
    13.             bookmark = (ImageView) findViewById(i);
    14.            
    15.             lp = (LayoutParams) bookmark.getLayoutParams();
    16.            
    17.             if(i > 0) {
    18.                 previousBookmark = (ImageView) findViewById(i - 1);
    19.                
    20.                  if(this.getWidth() > (previousBookmark.getLeft() + previousBookmark.getWidth())) {
    21.                      lp.addRule(RelativeLayout.RIGHT_OF, previousBookmark.getId());
    22.                  } else {
    23.                      lp.addRule(RelativeLayout.BELOW, idOfViewToTheLeft);
    24.                      idOfViewToTheLeft = bookmark.getId();
    25.                  }
    26.             }
    27.            
    28.             bookmark.setLayoutParams(lp);
    29.         }
    30.     }
     

    Advertisement

Share This Page

Loading...