The question is published on by Tutorial Guruji team.
I am trying to create an app that allows me to pick a date and a table is populated with appointments on that day. I am having troubles with getting the date picker to show.
I have taken a look at this https://developer.android.com/guide/topics/ui/controls/pickers and created a date picker from this code. I want to call that date picker from a button press found in the AddAppointmentActivity
class.
Here is my Appointment class:
package com.example.dentdevils.ui.HomeLinks; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.DialogFragment; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import com.example.dentdevils.R; import com.example.dentdevils.ui.HomeActivity; import java.util.Calendar; public class AddAppointmentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_appointment); // Date label TextView dateLabel = (TextView) findViewById(R.id.txtDateLabel); // Date Picker Button Functionality Button datePicker = (Button) findViewById(R.id.btnDatePicker); datePicker.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); // Back Button Functionality Button btnBack = (Button) findViewById(R.id.btnBack); btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent launchHomepage = new Intent(AddAppointmentActivity.this, HomeActivity.class); startActivity(launchHomepage); } }); } } class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { final Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(), this, year, month, day); } @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { // Get date set by user and display appointments in table for the date chosen. } public void showDatePickerDialog() { DialogFragment newFragment = new com.example.dentdevils.ui.HomeLinks.DatePickerFragment(); newFragment.show(getFragmentManager(), "datePicker"); } }
I did have the classes in separate files but was testing different things hence why they are in the same file (I will move them back out again to separate files after I have managed to call the date picker).
My question is, how would I call the date picker dialog from the button press?
Answer
datePicker.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getSupportFragmentManager(), "datePicker"); } });