50

Based on the Android document which doesn't give much explanation, what's the difference between setPadding() vs setPaddingRelative()?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jerrytouille
  • 1,238
  • 1
  • 13
  • 28

1 Answers1

69

setPaddingRelative has this code inside:

switch(getResolvedLayoutDirection()) {
        case LAYOUT_DIRECTION_RTL:
            setPadding(end, top, start, bottom);
            break;
        case LAYOUT_DIRECTION_LTR:
        default:
            setPadding(start, top, end, bottom);
}

So when you set padding with setPaddingRelative it changes left and right padding values depending on user's layout direction.

Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • 48
    God, they chosen a misleading name for that. I though that this means "adjust padding relative to the current values", so setPaddingRelative(0, 10, 5, 0) would increase top padding by 10 and right padding by 5. I really miss such function sometimes... – dimsuz Feb 02 '15 at 18:48
  • ViewCompat#setPaddingRelative can automatically call either setPaddingRelative if RTL is supported, or setPadding if it's not (i.e. pre-API 17), if you care about that sort of thing – Vasiliy Kulakov Jan 11 '19 at 13:13