It purely depends on what are you creating finally:
Use code when:
Just a few controls
A lot of controls but they inherit properties from each other based on some logic
Controls need creation or removal based on actions/interaction
Need performance (like loading a lot of slightly complex cards in a customlistview)
Best to Use designer when:
Complex Layouts
Involves Custom Views (not all custom views are code friendly)
Layouts are reused within or outside the project (more manageable)
Maintaining Consistency and Look and Feel across pages (easier to see, select, change or copy paste)
@William Lancee and
@angel_ have already given some examples.
Moreover it works the same way:
Declare a control
Set its properties
Add it to a panel or activity/Root