TY Practical No. 16 – Implement Date Picker in Android Application

 

Practical No. 16 – Implement Date Picker in Android Application


1. Project Information

Project Name: DatePickerApp

Language: Java
Minimum SDK: API 21 or above


2. File Structure

DatePickerApp
│
├── app
│   └── src
│       └── main
│           ├── java/com/example/datepickerapp
│           │       └── MainActivity.java
│           │
│           ├── res
│           │   ├── layout
│           │   │       └── activity_main.xml
│           │   │
│           │   └── values
│           │           └── strings.xml
│           │
│           └── AndroidManifest.xml

3. AndroidManifest.xml

Ensure the activity is declared.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.datepickerapp">

    <application
        android:allowBackup="true"
        android:label="Date 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>

4. Layout File

res/layout/activity_main.xml

This layout contains:

  • Student Name display

  • Selected Date display

  • Button to open Date Picker

<?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/txtStudentName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Student Name"
        android:textSize="22sp"
        android:textStyle="bold"
        android:layout_marginBottom="30dp"/>

    <TextView
        android:id="@+id/txtSelectedDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Selected Date: Not Selected"
        android:textSize="18sp"
        android:layout_marginBottom="30dp"/>

    <Button
        android:id="@+id/btnPickDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Date"/>

</LinearLayout>

5. Strings File

res/values/strings.xml

<resources>
    <string name="app_name">Date Picker App</string>
</resources>

6. MainActivity.java

java/com/example/datepickerapp/MainActivity.java

package com.example.datepickerapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    TextView txtStudentName, txtSelectedDate;
    Button btnPickDate;

    String firstName = "Rahul";
    String lastName = "Sharma";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtStudentName = findViewById(R.id.txtStudentName);
        txtSelectedDate = findViewById(R.id.txtSelectedDate);
        btnPickDate = findViewById(R.id.btnPickDate);

        // Display Student Name
        txtStudentName.setText("Student: " + firstName + " " + lastName);

        btnPickDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Calendar calendar = Calendar.getInstance();

                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog datePickerDialog = new DatePickerDialog(
                        MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view,
                                                  int selectedYear,
                                                  int selectedMonth,
                                                  int selectedDay) {

                                selectedMonth = selectedMonth + 1;

                                String date = selectedDay + "/" + selectedMonth + "/" + selectedYear;

                                txtSelectedDate.setText("Selected Date: " + date);
                            }
                        },
                        year, month, day
                );

                datePickerDialog.show();
            }
        });
    }
}

7. Expected Output :




Popular posts from this blog