Kurlicue Kurlicue - 6 months ago 45
Android Question

RelativeLayout.params getRule before API23

I'm trying to check a rule in RelativeLayout.LayoutParams, but getRule() was only introduced in API 23, is there a way to do this in the earlier versions?

Answer

There is another method similar to getRule() called getRules(), which was introduced in API Level 1. The documentation for the method is at: https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#getRules(). You can use this method similarly. Instead of passing the int, you pass nothing and get an array of supported rules. You can then loop through that array of rules and compare them with the rule you are looking for. Here is an example:

int[] rules = relativeLayout.LayoutParams.getRules();
for(int rule : rules) {
    if(rule == yourRule) {
        // Rule exists in RelativeLayout
    } else {
        // Rule doesn't exist
    }
}

Hope it helps!