Chiggins
Chiggins

Reputation: 8407

Displaying custom dialog

Alright, so I would like to have a custom dialog, but I cannot figure out for the life of me how to make it appear when the function is called.

public void addHomework() {
    final Dialog alert = new Dialog(this);  

    alert.setTitle("Add Homework");

    alert.setContentView(R.layout.homework_item_entry);

    Button add_button = (Button) findViewById(R.id.add_homework_button);
    Button cancel_button = (Button) findViewById(R.id.cancel_homework_button);

    add_button.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(ClassHomeworkList.this, "Adding homework", Toast.LENGTH_SHORT).show();
        }
    });

    cancel_button.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
            alert.dismiss();
        }
    });

    alert.show();
}

What could I do?

Upvotes: 1

Views: 1916

Answers (4)

Tom
Tom

Reputation: 1

LayoutInflater factory = LayoutInflater.from(this);
View view = factory.inflate(R.layout.dialog, null);

//the id is your root layout
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
alert.setContentView(layout);

Upvotes: 0

stealthcopter
stealthcopter

Reputation: 14206

I think you have the problem that your two buttons cannot be found by their ID's like this (as you are trying to find them in your main activity, but they are in the layout for the dialog)

Button add_button = (Button) findViewById(R.id.add_homework_button);
Button cancel_button = (Button) findViewById(R.id.cancel_homework_button);

But instead need to do:

Button add_button = (Button) alert.findViewById(R.id.add_homework_button);
Button cancel_button = (Button) alert.findViewById(R.id.cancel_homework_button);

Upvotes: 1

JAL
JAL

Reputation: 3319

I know this is an old thread, but even after reading the Android docs it also was not obvious to me how to display a custom dialog using the standard Dialog class. Basically you can call:

this.showDialog(MANAGE_PASSWORD); // MANAGE_PASSWORD static final int

from your activity. Then instantiate the custom dialog in the onCreateDialog method:

   protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch(id) {
        case MANAGE_PASSWORD:
            dialog= getInstancePasswordDialog();
            break;
        case DIALOG_ABOUT:
            // do the work to define the About Dialog
            dialog= getInstanceAlertDialog(); // called "the first time"
            break;
        default:
            dialog = null;
        }
        return dialog;
    }

The code to instantiate the dialog is in getInstancePasswordDialog(). Here is the code sample.

Upvotes: 4

dimsuz
dimsuz

Reputation: 9207

Have you read the following document: http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog ?

You should override your Activity's onCreateDialog(int) method as described there and then use showDialog(int)

Upvotes: 0

Related Questions