Search code examples
androidandroid-activitycustomdialog

Calling an activity from a custom dialog


I guess this is just a simple question (I’m such a noob…) I have this custom dialog box that has 3 buttons in it.

Now I want to call an activity from one of the buttons so I tried this:

public class picturedialog extends Dialog implements OnClickListener {
    Button Camera;

    public picturedialog (Context context){
        super (context);
        setContentView(R.layout.picturedialog);

        Camera = (Button) this.findViewById(R.id.pdButton1);

        Camera.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                dismiss();

                Intent myIntent = new Intent(view.getContext(), CameraActivity.class);
                startActivity(myIntent);

            }
        });
...
}

Then the red squiggly line appears on startActivity(myIntent). Upon hovering on it, eclipse tells me this: “The method startActivity(Intent) is undefined for the type new View.OnClickListener(){}” Ehhh? Please orient me on how to do this properly. Any help would be appreciated.


Solution

  • I am sure you are overcomplicating with subclassing from Dialog. Try to follow to the dialog tutorial - https://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog

    Note that dialogs are created on the fly (in Activity.onCreateDialog()) without the need to have your own custom dialog classes. Since you set a listener being inside of the wrapping activity (if you follow the tutorial), then you are able to call startActivity(myIntent), because all the fields/methods of a wrapping class instance are available for an instance of an inner class.