DatePicker is android widget. And choose from the date and time under this select the datepicker widget. We can select the year,month,day.
activitymain.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.kiranapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date :"
android:id="@+id/textView"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choose date"
android:id="@+id/button"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/datePicker"
android:layout_gravity="center_horizontal" />
</LinearLayout>
MainActivity.java :-
package com.kiranapp;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
Button selectbtn;
TextView displaydate;
Calendar c=Calendar.getInstance();
int cdate,cmonth,cyear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectbtn=(Button)findViewById(R.id.button);
displaydate=(TextView)findViewById(R.id.textView);
selectbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this,d1,c.get(Calendar.YEAR),
c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
DatePickerDialog.OnDateSetListener d1=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
cdate=dayOfMonth;
cmonth=monthOfYear+1;
cyear=year;
displaydate.setText("Date is :"+cdate+"-"+cmonth+"-"+cyear);
}
};
}
Output :-
activitymain.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.kiranapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date :"
android:id="@+id/textView"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choose date"
android:id="@+id/button"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/datePicker"
android:layout_gravity="center_horizontal" />
</LinearLayout>
MainActivity.java :-
package com.kiranapp;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
Button selectbtn;
TextView displaydate;
Calendar c=Calendar.getInstance();
int cdate,cmonth,cyear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectbtn=(Button)findViewById(R.id.button);
displaydate=(TextView)findViewById(R.id.textView);
selectbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this,d1,c.get(Calendar.YEAR),
c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
DatePickerDialog.OnDateSetListener d1=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
cdate=dayOfMonth;
cmonth=monthOfYear+1;
cyear=year;
displaydate.setText("Date is :"+cdate+"-"+cmonth+"-"+cyear);
}
};
}
Output :-
DatePicker fig :1 |
DatePicker fig :2 |
DatePicker fig:3 |
No comments:
Post a Comment