Search code examples
androidandroid-relativelayoutandroid-custom-view

Android RelativeLayout params ignored


I'm stuck with a weird issue : I have a class which extends from RelativeLayout.

Then I want to add it two Views like this way :

mButtonTip = new ButtonTip(context, attrs);
mToolTip = new ToolTip(context, attrs);

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_BOTTOM, mToolTip.getId());

addView(mToolTip, params1);
addView(mButtonTip, params2);

Unfortunately, the mToolTip View is correctly placed but the mButtonTip won't move from the bottom left of the layout.

Do you have any idea why the mButtonTip View is ignoring the Layout params I just set ?


Solution

  • All right, so for those may encounter the same issue, I found a solution :

    I needed to set explicitly the id of the different View like that :

    mButtonTip = new ButtonTip(context, attrs);
    mButtonTip.setId(1);
    
    mToolTip = new ToolTip(context, attrs);
    mToolTip.setId(2);
    
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    
    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params2.addRule(RelativeLayout.ALIGN_BOTTOM, mToolTip.getId());
    
    addView(mToolTip, params1);
    addView(mButtonTip, params2);