Regis St-Gelais Regis St-Gelais - 1 year ago 137
Android Question

How do I programmatically remove an existing rule that was defined in XML?

I have a linear layout that is contained inside a relative layout.
It is set in the XML file to be to the right of another linear layout (this works fine).
In some cases I want to change the relative position of the layout during the onCreate of the activity so I need to modify the "to the right of" param to relate to another layout.
I tryed this:

RelativeLayout.LayoutParams layoutParams;

layoutParams = (RelativeLayout.LayoutParams) linearLayoutToMove

But it does not work :o(

Any clues ?

Answer Source

You can't remove a rule because all rules are always stored in a fixed-size java array. But you can set a rule to 0. For example

layoutParams.addRule(RelativeLayout.RIGHT_OF, 0);

EDIT (thanks to Roger Rapid):

As of API level 17, the class RelativeLayout.LayoutParams has the following method:

public void removeRule(int verb) 

So you can remove a rule using the following line of code:


And you will get exactly the same result as when 'adding' a zero-rule as:

layoutParams.addRule(RelativeLayout.RIGHT_OF, 0);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download