0

I'm trying to pop up a custom dialog when I click on a button but it won't pop up at all. my app is basically a calendar and I'm going to use sqlite to add/hold appointments and stuff to a date in the calendar using the dialog, which is where the appointment details will be specified.

The code I'm using for this is the following:

public void onClick(View v) {
        // TODO Auto-generated method stub
        //long a = calendar.getDate();
        switch(v.getId()){
        case R.id.createButton:
            openCreateAppointmentDialog();
            break;
        }
    }

    private void openCreateAppointmentDialog(){
        Context mContext = getApplicationContext();
        Dialog createAppmntDialog = new Dialog(mContext);

        createAppmntDialog.setContentView(R.layout.create);
        createAppmntDialog.setTitle(R.string.createTitle);

        appointmentTitle = (EditText) createAppmntDialog.findViewById(R.id.titleTextBox);
        appointmentTitle.setText("hello");

        appointmentTime = (EditText) createAppmntDialog.findViewById(R.id.timeTextBox);

        appointmentDetails = (EditText) createAppmntDialog.findViewById(R.id.detailsTextBox);

        saveAppointment = (Button) createAppmntDialog.findViewById(R.id.saveButton);
        saveAppointment.setOnClickListener(this);
    }

What am I doing wrong?

a7omiton
  • 1,597
  • 4
  • 34
  • 61

1 Answers1

3

Call the show() method for your dialog.

createAppmntDialog.show(); //when you want the dialog to appear on the screen
user
  • 86,916
  • 18
  • 197
  • 190
  • thanks that worked hehe, but i got a runtime exception so something must be wrong in my code – a7omiton Apr 20 '12 at 09:14
  • @AZ1 And the `RuntimeException` is saying ...? – user Apr 20 '12 at 09:16
  • 04-20 09:16:03.286: E/AndroidRuntime(1188): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application – a7omiton Apr 20 '12 at 09:17
  • 1
    I removed the context from the dialog and just said ...new Dialog(this); and it worked, but is this safe for wanting to take the users input and store it in an sqlite database? – a7omiton Apr 20 '12 at 09:18
  • @AZ1 A dialog will not work with the application context, it's tied to the activity where is created and shown. It is safe to use `this`, there are no problems. – user Apr 20 '12 at 09:20
  • its weird though, on the android dev website it shows a tutorial where they want you to use context: http://developer.android.com/guide/topics/ui/dialogs.html, but maybe its for a different sort of app or somethin? – a7omiton Apr 20 '12 at 09:22
  • 1
    @AZ1 Well, the `Dialog` constructor requires a `Context` reference, just that it will not work with an `Application Context`. By passing `this`(with `this` you are referring to the current activity where you build the dialog) you are providing the current activity `Context` and this will work. – user Apr 20 '12 at 09:26