when to use the relative layout and constrain layout with real time example

Could someone please help me out letting me know when to use the relative layout and when to use the constrain layout with example in android.

First you need to know why ConstraintLayout is introduced later in Android SDK.

Why ConstraintLayout is introduced?

Using Relative and other layouts, we need to create a hierarchy of different views. Sometimes these hierarchy can be long with multiple nested view groups.

When Android renders layout, All the views will be rendered going to level below in nested layouts. So more levels, requires more time.

So According to Android Documentation,

ConstraintLayout allows you to create large and complex layouts with a
flat view hierarchy (no nested view groups).

Which can take lesses time than other nested views.

What is difference between ConstraintLayout and RelativeLayout?

ConstraintLayout is similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it’s more flexible than RelativeLayout.

When to choose which Layout?
If your screen has design such that you can do using no nested RelativeLayout, Use Relative or else use Constraint.