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

Date picker on button click

The document provides an XML layout and Java code for an Android application that includes a DatePicker and a button. When the button is clicked, it updates a TextView to display the selected date from the DatePicker. The application initializes with the current date displayed in the TextView.

Uploaded by

solankesnehal96k
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)
4 views

Date picker on button click

The document provides an XML layout and Java code for an Android application that includes a DatePicker and a button. When the button is clicked, it updates a TextView to display the selected date from the DatePicker. The application initializes with the current date displayed in the TextView.

Uploaded by

solankesnehal96k
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/ 2

Date picker on button click ( simple )

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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_marginBottom="102dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:text="" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Change Date"/>

<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_marginBottom="36dp" />
</LinearLayout>

Java file -:

package com.example.uefclass;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {


DatePicker picker;
Button displayDate;
TextView textview1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview1=(TextView)findViewById(R.id.textView1);
picker=(DatePicker)findViewById(R.id.datePicker);
displayDate=(Button)findViewById(R.id.button1);

textview1.setText("Current Date: "+getCurrentDate());

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

textview1.setText("Change Date: "+getCurrentDate());


}

});

}
public String getCurrentDate(){
StringBuilder builder=new StringBuilder();;
builder.append((picker.getMonth() + 1)+"/");//month is 0 based
builder.append(picker.getDayOfMonth()+"/");
builder.append(picker.getYear());
return builder.toString();
}
}

You might also like