0% found this document useful (0 votes)
24 views

Practical 16

The document shows an Android code sample to display a date and time picker dialog on click of buttons. The XML layout contains two EditText fields and two buttons to select date and time. The Java code implements onClick listeners for the buttons to show DatePickerDialog and TimePickerDialog respectively, and set the result to the EditText fields.

Uploaded by

Nisha Parchande
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)
24 views

Practical 16

The document shows an Android code sample to display a date and time picker dialog on click of buttons. The XML layout contains two EditText fields and two buttons to select date and time. The Java code implements onClick listeners for the buttons to show DatePickerDialog and TimePickerDialog respectively, and set the result to the EditText fields.

Uploaded by

Nisha Parchande
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/ 3

Practical 16

Q2. WAP to display following output. Select and display date and time on click of “Select
Date”, “Select Time” buttons respectively.
XML code:

<?xml version="1.0" encoding="utf-8"?> android:layout_marginTop="21dp"


<androidx.constraintlayout.widget.ConstraintL android:layout_marginEnd="8dp"
ayout android:ems="10"
xmlns:android="http://schemas.android.com/ android:inputType=""
apk/res/android" android:hint="This will show Time"
android:text=""
xmlns:app="http://schemas.android.com/apk/
res-auto" app:layout_constraintEnd_toStartOf="@+id/b
utton2"
xmlns:tools="http://schemas.android.com/too
ls" app:layout_constraintHorizontal_chainStyle="
android:layout_width="match_parent" packed"
android:layout_height="match_parent"
tools:context=".MainActivity"> app:layout_constraintStart_toStartOf="parent
"
<EditText
android:id="@+id/editText" app:layout_constraintTop_toBottomOf="@+id
android:layout_width="wrap_content" /editText" />
android:layout_height="wrap_content"
android:layout_marginTop="134dp" <Button
android:layout_marginEnd="5dp" android:id="@+id/button"
android:ems="10" android:layout_width="wrap_content"
android:inputType="" android:layout_height="wrap_content"
android:hint="This will show Date" android:layout_marginEnd="18dp"
android:text="" android:layout_marginBottom="6dp"
android:text="Select Date"
app:layout_constraintEnd_toStartOf="@+id/b
utton" app:layout_constraintBottom_toBottomOf="
@+id/editText"
app:layout_constraintHorizontal_chainStyle="
packed" app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent app:layout_constraintStart_toEndOf="@+id/e
" ditText" />

app:layout_constraintTop_toTopOf="parent" <Button
/> android:id="@+id/button2"
android:layout_width="wrap_content"
<EditText android:layout_height="wrap_content"
android:id="@+id/editText2" android:layout_marginEnd="12dp"
android:layout_width="wrap_content" android:layout_marginBottom="7dp"
android:layout_height="wrap_content" android:text="Select Time"
app:layout_constraintBottom_toBottomOf=" app:layout_constraintStart_toEndOf="@+id/e
@+id/editText2" ditText2" />
</androidx.constraintlayout.widget.Constraint
app:layout_constraintEnd_toEndOf="parent" Layout>

Java Code:
package com.example.prac161;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import java.util.Calendar;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity


implements View.OnClickListener{
Button btn1, btn2;
EditText et1, et2;
private int mYear, mMonth, mDay, mHour, mMinute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn1=(Button)findViewById(R.id.button);
btn2=(Button)findViewById(R.id.button2);
et1=(EditText)findViewById(R.id.editText);
et2=(EditText)findViewById(R.id.editText2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==btn1){
final Calendar c= Calendar.getInstance();
mYear=c.get(Calendar.YEAR);
mMonth=c.get(Calendar.MONTH);
mDay=c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog=
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
et1.setText(dayOfMonth+ "-" +(monthOfYear +1) + "-" +year);
}
},mYear, mMonth,mDay);
datePickerDialog.show();
}
if(v==btn2){
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
TimePickerDialog tp = new TimePickerDialog(this, new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
et2.setText(hourOfDay+":"+minute);
}
},mHour, mMinute, false);
tp.show();
}
}
}

You might also like