0

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?

AF_web
  • 89
  • 11
  • already have answer here https://stackoverflow.com/questions/39916178/how-to-show-datepickerdialog-on-button-click – Shalu T D Jun 24 '20 at 10:28
  • 2
    Does this answer your question? [Datepicker: How to popup datepicker when click on edittext](https://stackoverflow.com/questions/14933330/datepicker-how-to-popup-datepicker-when-click-on-edittext) – Shalu T D Jun 24 '20 at 10:32

2 Answers2

0
datePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getSupportFragmentManager(), "datePicker");


        }
    });
heyjii
  • 834
  • 8
  • 26
0

You don't need create an extra dialog fragment for date picker. Just create DatePickerDialog object and call show().

Example:

val calendar = Calendar.getInstance()

val dialog = DatePickerDialog(context,
                    DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                        // TODO
                    }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH))

dialog.show()
lbasek
  • 389
  • 3
  • 12