Last active
April 26, 2018 05:25
-
-
Save ajithvgiri/0eec447eea1a30481b97125aef54aa52 to your computer and use it in GitHub Desktop.
DatePicker Dialog in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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