activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:timePickerMode="clock"
        android:layout_marginTop="20dp"

        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="80dp"
        app:layout_constraintTop_toBottomOf="@id/timePicker"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:textSize="30dp"
        tools:layout_editor_absoluteX="10dp"
        tools:layout_editor_absoluteY="399dp" />



</android.support.constraint.ConstraintLayout>

---------------------------------------------------------------------------

MainActivity.kt :


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setupTimePicker()
     }
    }

    private fun setupTimePicker(){
        val textView=findViewById<TextView>(R.id.textView)
        val timePicker=findViewById<TimePicker>(R.id.timePicker)
        timePicker.setOnTimeChangedListener {_,hour,minute ->
            val period =if(hour>= 12) "PM" else "AM"
            val formatedhour= if(hour==0) 12 else if(hour> 12) hour - 12 else hour
            val formatedminute= String.format("%02d",minute)

            val msg= "Time is: $formatedhour : $formatedminute $period"
            textView.text=msg
            textView.visibility=View.VISIBLE
        }
    }
