0% found this document useful (0 votes)
16 views2 pages

Practical-N0 16

The document describes a program to implement a date picker in Android. It includes Java code to create a date picker, get the selected date, and display it in a toast message. It also includes the XML layout file to display the date picker and button.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Practical-N0 16

The document describes a program to implement a date picker in Android. It includes Java code to create a date picker, get the selected date, and display it in a toast message. It also includes the XML layout file to display the date picker and button.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

------------------------------------------------------------------------------------------

Practical No: 16
Title: Develop a program to implement Date and Time Picker
class: TYCO Roll No: 12

// Date Picker Mode

package com.example.date_picker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button btn;
DatePicker dp;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.bt);
dp=(DatePicker)findViewById(R.id.dp);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// get the values for day of month , month and


year from a date picker
String day = "Day = " + dp.getDayOfMonth();
String month = "Month = " + (dp.getMonth()+1);
String year = "Year = " + dp.getYear();
// display the values by using a toast
Toast.makeText(getApplicationContext(), day +
"\n" + month + "\n" + year, Toast.LENGTH_LONG).show();
}
});

}
}

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dp"
android:layout_centerHorizontal="true"
android:padding="20dp"
android:datePickerMode="calendar"
></DatePicker>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt"
android:text="Submit"
android:textColor="#fff"
android:background="#150"
android:layout_marginTop="500dp"
android:layout_marginLeft="180dp"
android:textSize="20sp"
android:textStyle="bold"
></Button>

</RelativeLayout>

Fig. Date Picker

You might also like