Android Program : Create an android application that demonstrate date picker and datepickerdialog.

 

create an android application that demonstrate date picker and datepickerdialog.


Soln :

activity_main.xml



<?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"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<Button
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="select date"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="18sp"
android:layout_margin="20dp"
android:padding="10dp"/>

<Button
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="select date"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="18sp"
android:layout_margin="20dp"
android:padding="10dp"/>

</LinearLayout>

<TextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:text="Date"
android:gravity="center"
android:layout_marginTop="40dp"/>

<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:text="Time"
android:gravity="center"
android:layout_marginTop="20dp"/>


</LinearLayout>



MainActivity.java


package com.example.slipap2b;

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 {
private int year, month, day, hour, minute;
TextView tv_date, tv_time;
Button date, time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_date = findViewById(R.id.tv_date);
tv_time = findViewById(R.id.tv_time);
date = findViewById(R.id.date);
time = findViewById(R.id.time);

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

final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog picker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

}


}, year, month,day);
picker.show();
}
});

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

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

TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker timePicker, int hour, int min) {
tv_time.setText(hour+":"+min);
}
},hour,minute,false);
timePickerDialog.show();
}
});
}
}



ScreenShot








Post a Comment

0 Comments