Code&Output
Code&Output
Xml File
<?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">
<TextView
android:id="@+id/selectedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Date: "
android:textSize="18sp"
android:textColor="@color/black"
android:paddingBottom="10dp"/>
<Button
android:id="@+id/btnPickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Date"
android:backgroundTint="@color/purple"
android:textColor="@android:color/white"/>
<TextView
android:id="@+id/selectedTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Time: "
android:textSize="18sp"
android:textColor="@color/black"
android:paddingTop="10dp"/>
<Button
android:id="@+id/btnPickTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Time"
android:backgroundTint="@color/purple"
android:textColor="@android:color/white"/>
<EditText
android:id="@+id/noteInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your reminder..."
android:padding="10dp"
android:background="@android:drawable/edit_text"
android:textSize="16sp"
android:layout_marginTop="10dp"
android:minHeight="50dp"/>
<Button
android:id="@+id/btnSaveReminder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Reminder"
android:backgroundTint="@color/green"
android:textColor="@android:color/white"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/savedReminder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="16sp"
android:paddingTop="10dp"
android:visibility="gone"
android:gravity="center"/>
</LinearLayout>
Colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple">#6200EE</color>
<color name="green">#4CAF50</color>
<color name="white">#FFFFFF</color>
<color name="lightGray">#F5F5F5</color>
<color name="black">#000000</color>
</resources>
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">DateandTime2</string>
<string name="title_text">Date & Time Picker</string>
<string name="pick_date">Pick Date</string>
<string name="pick_time">Pick Time</string>
<string name="selected_date">Selected Date: </string>
<string name="selected_time">Selected Time: </string>
<string name="save_note">Save Reminder</string>
<string name="add_note_hint">Add a note...</string>
<string name="reminder_saved">Reminder Saved!</string>
<string name="enter_note">Please enter a note!</string>
<string name="save_reminder">Save Reminder</string>
<string name="enter_reminder">Enter your reminder</string>
<string name="no_reminder">No reminder set yet</string>
</resources>
Java File
package com.example.dateandtime2;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedDate = findViewById(R.id.selectedDate);
selectedTime = findViewById(R.id.selectedTime);
noteInput = findViewById(R.id.noteInput);
btnPickDate = findViewById(R.id.btnPickDate);
btnPickTime = findViewById(R.id.btnPickTime);
btnSaveReminder = findViewById(R.id.btnSaveReminder);
savedReminder = findViewById(R.id.savedReminder);
// Pick Date
btnPickDate.setOnClickListener(v -> showDatePicker());
// Pick Time
btnPickTime.setOnClickListener(v -> showTimePicker());
// Save Reminder
btnSaveReminder.setOnClickListener(v -> saveReminder());
}
if (noteText.isEmpty()) {
Toast.makeText(this, "Please enter a reminder",
Toast.LENGTH_SHORT).show();
return;
}
if (selectedDateString.isEmpty() || selectedTimeString.isEmpty()) {
Toast.makeText(this, "Please select a date and time first",
Toast.LENGTH_SHORT).show();
return;
}