So, in the last few days, several teammates have asked me what is the difference between paddingLeft and paddingStart. So, I have written this to document my experience.
Why use paddingStart?
paddingRelative, paddingStart and paddingEnd are added in API 17 to support RTL devices. When your phone is switched to a RTL language, every view should be a mirror image of LTR mode.
In a LTR language, paddingStart is on the left and paddingEnd is on the right. In a RTL language (Arabic, Hebrew), it’s the opposite. If you only define paddingLeft and paddingRight and not start and end, then the layout would not be a mirror image in a RTL phone.
When to use paddingStart?
paddingStart and paddingEnd are equivalent to paddingLeft and paddingRight in API 17+ devices. Remember to add paddingStart and paddingEnd in these situations:
- When paddingLeft and paddingRight are different.
- Your app needs to support API 17+ devices with RTL.
How to use paddingStart and paddingEnd?
- If paddingLeft and paddingRight are the same, you are done.
- Otherwise, wherever you see a paddingLeft, put a paddingStart with it, same for right-end.
- Do not remove paddingLeft and paddingRight from xml file (until the day we decide we will stop supporting API 16- devices).
<TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="@dimen/vertical_padding" android:paddingEnd="@dimen/end_padding" android:paddingRight=""@dimen/end_padding" android:paddingBottom="@dimen/vertical_padding" android:paddingStart="@dimen/start_padding" android:paddingLeft="@dimen/start_padding" />
I used paddingStart and paddingEnd and now QA tells me that my padding is broken in ICS/JB devices.
RTL support is added in API 17, so it’s still necessary to define paddingLeft and paddingRight in your xml. paddingStart and paddingEnd will override those values in API 17+ device.
Ok, it fails the lint test. Are you lying to me?
As I have said in “Why use paddingStart?”, paddingStart (etc) are added in API 17. So it’s normal for lint test to fail. Here’s a few solutions:
- Equal left and right paddings
- If left padding is equal to right padding, then we don’t need to define start and end paddings.
- Suppress the lint error
- Add xmlns:tools=”http://schemas.android.com/tools” and tools:ignore=”NewApi” to the xml file
- Have a separate style file
- Define a values-v17 folder
- Place start and end in values-v17
- Keep left and right in values.