You are on page 1of 3

16th Practical MAD:

XML CODE:
<?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:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<DatePicker
android:id="@+id/date_pick"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="800px"
android:layout_height="wrap_content"/>

<TextView
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center"
android:id="@+id/date_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/btn"
android:text="Submit"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>
JAVA FILE:

package com.example.datepickerdemoandspinnerpicker;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.*;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


DatePicker date_pick;
TextView date_here;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.btn);
date_here=findViewById(R.id.date_here);
date_pick=findViewById(R.id.date_pick);
date_here.setText("Current Date:
"+getDateHere());
btn.setOnClickListener(new
View.OnClickListener(){
public void onClick(View v){
date_here.setText("Selected Date:
"+getDateHere());
}
});
}
public String getDateHere(){
String
str=""+date_pick.getDayOfMonth()+"/"+(date_pick.getMont
h()+1)+"/"+date_pick.getYear()+"";
return str;
}
}

You might also like