Refactors for readability and adds an API >M check to be compatible with the LinearLayout fix that also targets API >M. This revert commit reverts revert commit9d8a230fbdwhich originally reverted commit9cefbda11e. Change-Id: I587d733abef0b35a1bb14b6272054322494a7cdd