0% found this document useful (0 votes)
4 views10 pages

Code&Output

The document contains an Android application code that allows users to pick a date and time, enter a reminder note, and save it. It includes XML layout files for the user interface, color resources, string resources, and Java code for the main activity functionality. The app features buttons for date and time selection, an input field for reminders, and displays the saved reminder on the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Code&Output

The document contains an Android application code that allows users to pick a date and time, enter a reminder note, and save it. It includes XML layout files for the user interface, color resources, string resources, and Java code for the main activity functionality. The app features buttons for date and time selection, an input field for reminders, and displays the saved reminder on the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CODE

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 &amp; 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;

public class MainActivity extends AppCompatActivity {

private TextView selectedDate, selectedTime, savedReminder;


private EditText noteInput;
private Button btnPickDate, btnPickTime, btnSaveReminder;

private int year, month, day, hour, minute;


private String selectedDateString = "", selectedTimeString = "";

@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);

savedReminder.setVisibility(View.GONE); // Hide initially

// Pick Date
btnPickDate.setOnClickListener(v -> showDatePicker());

// Pick Time
btnPickTime.setOnClickListener(v -> showTimePicker());

// Save Reminder
btnSaveReminder.setOnClickListener(v -> saveReminder());
}

private void showDatePicker() {


final Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
(view, year, monthOfYear, dayOfMonth) -> {
selectedDateString = dayOfMonth + "/" + (monthOfYear + 1) + "/"
+ year;
selectedDate.setText("Selected Date: " + selectedDateString);
}, year, month, day);
datePickerDialog.show();
}

private void showTimePicker() {


final Calendar calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);

TimePickerDialog timePickerDialog = new TimePickerDialog(this,


(view, hourOfDay, minute) -> {
selectedTimeString = hourOfDay + ":" + String.format("%02d",
minute);
selectedTime.setText("Selected Time: " + selectedTimeString);
}, hour, minute, false);
timePickerDialog.show();
}

private void saveReminder() {


String noteText = noteInput.getText().toString().trim();

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;
}

// Display the reminder under the text field


String reminderText = "Reminder: " + noteText + "\nDate: " +
selectedDateString + "\nTime: " + selectedTimeString;
savedReminder.setText(reminderText);
savedReminder.setVisibility(View.VISIBLE);

// Clear the input field


noteInput.setText("");
}
}
OUTPUT

You might also like