Skip to content

Instantly share code, notes, and snippets.

@ajithvgiri
Last active April 26, 2018 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajithvgiri/0eec447eea1a30481b97125aef54aa52 to your computer and use it in GitHub Desktop.
Save ajithvgiri/0eec447eea1a30481b97125aef54aa52 to your computer and use it in GitHub Desktop.
DatePicker Dialog in Java
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Get Current Date
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity2.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateTextView.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
//DatePicker inside a Dialog
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity2.this);
View promptView = layoutInflater.inflate(R.layout.dialog_add, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity2.this);
// set prompts.xml to be the layout file of the alertdialog builder
alertDialogBuilder.setView(promptView);
final Button dateboughtbookButton=(Button) promptView.findViewById(R.id.calendar_button);
// Show a datepicker when the dateButton is clicked
dateboughtbookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Calendar now = Calendar.getInstance();
final Calendar c = Calendar.getInstance();
DatePickerDialog dpd = new DatePickerDialog(MainActivity2.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateboughtbookButton.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DATE));
dpd.show();
}
}
);
alertDialogBuilder
.setCancelable(false)
.
setPositiveButton("Add Alarm",new DialogInterface.OnClickListener() {
@Override
public void onClick (DialogInterface dialog,int id){
}
}
)
.
setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick (DialogInterface dialog,int id){
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment