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

Date ANDTime

The document contains an XML layout file and Java code for an Android application that allows a user to select a date and time using date and time pickers. The layout contains text views to display the selected date and time, date and time pickers, and buttons to trigger selecting the date and time.

Uploaded by

Maric Brass
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Date ANDTime

The document contains an XML layout file and Java code for an Android application that allows a user to select a date and time using date and time pickers. The layout contains text views to display the selected date and time, date and time pickers, and buttons to trigger selecting the date and time.

Uploaded by

Maric Brass
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

>
<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvDate"
android:layout_width="149dp"
android:layout_height="46dp"

android:textSize="20dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="date"
android:text="Set Date"
/>
<DatePicker
android:id="@+id/dtpcker"
android:layout_width="wrap_content"
android:layout_height="match_parent"

android:datePickerMode="spinner"
/>
<TimePicker
android:id="@+id/timepcker"
android:layout_width="184dp"
android:layout_height="195dp"

android:timePickerMode="spinner"
" />
<TextView
android:id="@+id/tvTime"
android:layout_width="130dp"
android:layout_height="56dp"

android:textSize="20dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="time"
android:text="Set Time"
/>

</LinearLayout>

import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TextView tvDate,tvTime;
DatePicker dtpcker;
TimePicker timepcker;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvDate = findViewById(R.id.tvDate);
tvTime = findViewById(R.id.tvTime);
b1 = findViewById(R.id.btnDate);
b2 = findViewById(R.id.btnTime);
dtpcker = findViewById(R.id.dtpcker);
timepcker = findViewById(R.id.timepcker);
}
public void date(View v) {
tvDate.setText("Date : "+dtpcker.getDayOfMonth()+"-"+dtpcker.getMonth()
+"-"+dtpcker.getYear());
}

public void time(View v) {


tvTime.setText(timepcker.getCurrentHour()
+":"+timepcker.getCurrentMinute());

}
}

You might also like