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

Android DatePicker Example - Javatpoint

The document discusses an Android DatePicker example in 3 sentences: It describes how to create an Android date picker widget that allows a user to select a date and displays it in a text view. The date picker is added to an XML layout file and linked to a Java activity class that retrieves the selected date and displays it when a button is clicked. The example demonstrates the basic use of a date picker to select and display a date in an Android application.

Uploaded by

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

Android DatePicker Example - Javatpoint

The document discusses an Android DatePicker example in 3 sentences: It describes how to create an Android date picker widget that allows a user to select a date and displays it in a text view. The date picker is added to an XML layout file and linked to a Java activity class that retrieves the selected date and displays it when a button is clicked. The example demonstrates the basic use of a date picker to select and display a date in an Android application.

Uploaded by

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

1/10/2021 Android DatePicker Example - javatpoint

Android DatePicker Example


Android DatePicker is a widget to select date. It allows
you to select date by day, month and year. Like
DatePicker, android also provides TimePicker to select
time.

The android.widget.DatePicker is the subclass of


FrameLayout class.

Android DatePicker Example


Let's see the simple example of datepicker widget in
android.

activity_main.xml

File: activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

https://www.javatpoint.com/android-datepicker-example 1/7
1/10/2021 Android DatePicker Example - javatpoint

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="example.javatpoint.com.datepicker.MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
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_alignParentBottom="true"
android:layout_centerHorizontal="true"
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_above="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp" />

</RelativeLayout>

https://www.javatpoint.com/android-datepicker-example 2/7
1/10/2021 Android DatePicker Example - javatpoint

Activity class

File: MainActivity.java

package example.javatpoint.com.datepicker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

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) {

https://www.javatpoint.com/android-datepicker-example 3/7
1/10/2021 Android DatePicker Example - javatpoint

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();
}
}

Output:

← prev next →

Youtube For Videos Join Our Youtube Channel:


Join Now

https://www.javatpoint.com/android-datepicker-example 4/7
1/10/2021 Android DatePicker Example - javatpoint

Help Others, Please Share

Learn Latest Tutorials

Apache Solr MongoDB Gimp Tutorial


Tutorial tutorial
Gimp
Solr MongoDB

Verilog Teradata PhoneGap


Tutorial Tutorial Tutorial

Verilog Teradata PhoneGap

Gmail Tutorial Vue.js Tutorial PLC tutorial

Gmail Vue.js PLC

Adobe Postman
Illustrator Tutorial
Tutorial
Postman
Illustrator

Preparation

Aptitude Logical Verbal Ability


Reasoning
https://www.javatpoint.com/android-datepicker-example 5/7
1/10/2021 Android DatePicker Example - javatpoint
Aptitude Reasoning Verbal A.

Interview Company
Questions Interview
Questions
Interview
Company

Trending Technologies

Artificial AWS Tutorial Selenium


Intelligence tutorial
Tutorial AWS
Selenium
AI

Cloud tutorial Hadoop ReactJS


tutorial Tutorial
Cloud
Hadoop ReactJS

Data Science Angular 7 Blockchain


Tutorial Tutorial Tutorial

D. Science Angular 7 Blockchain

Git Tutorial Machine DevOps


Learning Tutorial Tutorial
Git
ML DevOps

B.Tech / MCA

DBMS tutorial Data DAA tutorial


Structures
DBMS tutorial DAA

DS

Operating Computer Compiler


System tutorial Network tutorial Design tutorial

https://www.javatpoint.com/android-datepicker-example 6/7
1/10/2021 Android DatePicker Example - javatpoint
OS C. Network Compiler D.

Computer Discrete Ethical


Organization and Mathematics Hacking Tutorial
Architecture Tutorial
E. Hacking
COA D. Math.

Computer Software html tutorial


Graphics Tutorial Engineering
Tutorial Web Tech.
C. Graphics
Software E.

Cyber Automata C Language


Security tutorial Tutorial tutorial

Cyber Sec. Automata C

C++ tutorial Java tutorial .Net


Framework
C++ Java tutorial

.Net

Python tutorial List of Control


Programs Systems tutorial
Python
Programs Control S.

Data Mining
Tutorial

Data Mining

https://www.javatpoint.com/android-datepicker-example 7/7

You might also like