Date picker inside custom dialog in Android

8.8k views Asked by At

I wanted to use date picker inside my custom dialog. On button click calendar will open to choose date for user. I have Button inside my customDilaog class and on that button click I want to open calendar view. My app crashes if click this button. I have done with this.

CustomDialog.java

public class CustomDialog extends Dialog implements android.view.View.OnClickListener {

     private Button date;
     DateFormat format = DateFormat.getDateInstance();
     Calendar calender = Calendar.getInstance();

     public CustomDialog(Context context) {
         super(context);
     }

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
              date=(Button)findViewById(R.id.dateText2);
     }
     public void onClick(View v) {
         if (v.getId()==R.id.datePick) {
             openDatePicker(v);
         //another if-else statements
         }

     public void updateDate(){
          date.setText(format.format(calender.getTime()));
     }

     public void setDate(){
           new DatePickerDialog(getContext(),dp,calender.get(Calendar.YEAR) ,calender.get(Calendar.MONTH),calender.get(Calendar.DAY_OF_MONTH)).show();                   
     }

         DatePickerDialog.OnDateSetListener dp = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
                calender.set(Calendar.YEAR,year);
                calender.set(Calendar.MONTH, monthOfYear);
                calender.set(Calendar.DAY_OF_MONTH, dayOfMonth);
               updateDate();                
            }
        };
     }

I don't know where I made mistake.

1

There are 1 answers

0
Goodlife On BEST ANSWER

use this appropriately

 @OnClick(R.id.fab)
public void fabbuttonclicked(){
    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(context.getContext());

      View promptView = layoutInflater.inflate(R.layout.dialog_add_new_book, null);


    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context.getContext());

    // set prompts.xml to be the layout file of the alertdialog builder
    alertDialogBuilder.setView(promptView);

    final EditText titlebookEditText = (EditText) promptView.findViewById(R.id.book_title_edit_text);
    final EditText authorbookEditText=(EditText) promptView.findViewById(R.id.book_author_edit_text);
    final Button dateboughtbookButton=(Button) promptView.findViewById(R.id.book_datebought_button);
    final EditText conditionbookEditText=(EditText) promptView.findViewById(R.id.book_condition_edit_text);
    final EditText urlimagebookEditText=(EditText) promptView.findViewById(R.id.book_Image_edit_text);

// 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(context.getContext(),
                    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){
                String titlebook = titlebookEditText.getText().toString();
                String authorbook = authorbookEditText.getText().toString();
                String dateboughtbook = dateboughtbookButton.getText().toString();
                String conditionbook = conditionbookEditText.getText().toString();
                String urlimagebook = urlimagebookEditText.getText().toString();


                if (titlebook.isEmpty()) {
                    Toast.makeText(getActivity(), " Amount is required ! \n Alarm not set", Toast.LENGTH_LONG).show();
                    return;
                } else {
                    int nextID;
                    nextID = (int) (realm.where(Book.class).maximumInt("bookId") + 1);
                    Book book = new Book(nextID, titlebook, authorbook, dateboughtbook, conditionbook, urlimagebook);

                    realm.beginTransaction();
                    realm.copyToRealmOrUpdate(book);
                    realm.commitTransaction();

                    dialog.dismiss();
                    loadBooks();
                    Toast.makeText(getActivity(), " New BOOK added ! \n ", Toast.LENGTH_LONG).show();

                    //Toast.makeText(getActivity(), "Alarm is Set", Toast.LENGTH_LONG).show();
                }
            }
        }

        )
                .

        setNegativeButton("Cancel",
                                  new DialogInterface.OnClickListener() {
            public void onClick (DialogInterface dialog,int id){
                dialog.cancel();
            }
        });

    // create an alert dialog
    AlertDialog alertD = alertDialogBuilder.create();

    alertD.show();

}