mobile – Android programming: When should you define custom attributes that describe your View in attrs.xml rather than the Java class itself?

Recently I stumbled across this article on the official Android Developer site:
https://developer.android.com/training/custom-views/create-view#customattr

It explains how you can use XML to set specific attributes of your View class.
You could, for instance, define if a text is left or right aligned within your layout.xml file of the Activity or Fragment.
You could, however, also write a setter in the Java class.

Is there a recommendation when to use the one over the other?