Time Picker
Time Picker
Open the activity_main.xml layout file and add the TimePicker widget.
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_centerInParent="true"
android:timePickerMode="spinner" />
</RelativeLayout>
java
import android.os.Bundle;
import android.widget.TimePicker;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = findViewById(R.id.timePicker);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
});
}
Run your Android application on an emulator or a physical device. You should see a TimePicker widget
where you can select a time. Once a time is selected, a toast message will display the selected time.
That's it! You have successfully implemented a simple Time Picker in your Android application. You can
further customize the Time Picker widget and handle the selected time as needed for your application.