Issue
I've got a list of elements displayed. When I click on one element, a detail view is opening and displaying name, description etc. Also a date shall be changeable in the detail view.
For now, the date is set to today's date when I click, but the DatePicker does not show up (init method fails). I know I am doing something wrong and that there has to be an intent/an activity to start from my DetailView class, since it's not the MainActivity. But unfortunately, I have no idea how to do it.
This is my code in DetailView.java:
public void onDateChange() {
initDatePicker();
dateButton = findViewById(R.id.itemDateButton);
dateButton.setText(getTodaysDate());
}
private String getTodaysDate() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
month = month + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
return makeDateString(day, month, year);
}
private void initDatePicker() {
DatePickerDialog.OnDateSetListener dataSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
String date = makeDateString(dayOfMonth, month, year);
dateButton.setText(date);
}
};
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int style = AlertDialog.THEME_HOLO_LIGHT;
datePickerDialog = new DatePickerDialog(this, style, dataSetListener, year, month, day);
}
public void openDatePicker(View view) {
datePickerDialog.show();
}
The xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemDateText"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="Date"
app:layout_constraintTop_toBottomOf="@id/itemDescriptionWrapper"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemDateButton"
android:text="Change date"
style="?android:spinnerStyle"
app:layout_constraintTop_toBottomOf="@id/itemDateText"
android:onClick="@{() -> controller.onDateChange()}"
/>
I'm also able to send mails and sms from the DetailView by intent. But i just don't know how to start a DatePicker intent.
Any ideas? I would be very grateful. :)
Best regards, Jess
Solution
To show the DatePicker, All you require is to add .show()
method of DatePickerDialog
inside your initDatePicker()
function.
datePickerDialog = new DatePickerDialog(this, style, dataSetListener, year, month, day);
datePickerDialog.show();
Answered By - Nikunj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.