TY Practical No. 17 – Implement Time Picker in Android Application
Practical No. 17 – Implement Time Picker in Android Application
1. Project Structure
TimePickerApp
│
├── app
│ └── src
│ └── main
│ │
│ ├── java
│ │ └── com.example.timepickerapp
│ │ └── MainActivity.java
│ │
│ ├── res
│ │ ├── layout
│ │ │ └── activity_main.xml
│ │ │
│ │ └── values
│ │ ├── strings.xml
│ │ └── themes.xml
│ │
│ └── AndroidManifest.xml
2. activity_main.xml
Location
app → res → layout → activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">
<!-- Student Name -->
<TextView
android:id="@+id/tvStudent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student: Rahul Sharma"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="20dp"/>
<!-- Selected Time -->
<TextView
android:id="@+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Time: --:--"
android:textSize="18sp"
android:layout_marginBottom="30dp"/>
<!-- Button -->
<Button
android:id="@+id/btnPickTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Time"/>
</LinearLayout>
3. MainActivity.java
Location
app → java → com.example.timepickerapp → MainActivity.java
package com.example.timepickerapp;
import androidx.appcompat.app.AppCompatActivity;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TextView tvTime, tvStudent;
Button btnPickTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvStudent = findViewById(R.id.tvStudent);
tvTime = findViewById(R.id.tvTime);
btnPickTime = findViewById(R.id.btnPickTime);
btnPickTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(
MainActivity.this,
(view, hourOfDay, minute1) -> {
String ampm;
int hour12;
if (hourOfDay >= 12) {
ampm = "PM";
hour12 = hourOfDay - 12;
} else {
ampm = "AM";
hour12 = hourOfDay;
}
if (hour12 == 0)
hour12 = 12;
String time = hour12 + ":" + String.format("%02d", minute1) + " " + ampm;
tvTime.setText("Selected Time: " + time);
},
hour,
minute,
false // false = 12 hour format
);
timePickerDialog.show();
}
});
}
}
4. AndroidManifest.xml
app → manifests → AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timepickerapp">
<application
android:allowBackup="true"
android:label="Time Picker App"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
5. strings.xml
res → values → strings.xml
<resources>
<string name="app_name">Time Picker App</string>
</resources>
6. How the Program Works
Application starts.
Student name is displayed on the screen.
User presses Select Time button.
TimePickerDialog opens.
User selects Hour and Minute.
Selected time is displayed on the screen in 12-hour AM/PM format.
Example Output:
Student: Rahul Sharma
Selected Time: 12:30 PM
[ Select Time ]