tags:

views:

49

answers:

3

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?

A: 

I think you have the problem that your two buttons cannot be found by thier 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);
stealthcopter
A: 

LayoutInflater factory = LayoutInflater.from(this); View view = factory.inflate(R.layout.dialog, null); LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout); //the id is your root layout alert.setContentView(layout);

Tom
A: 

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)

dpimka