tags:

views:

611

answers:

1

I have an Android Activity that uses a class that I developed that extends a View (let's call it FooView). I'd like to launch a dialog box when a particular event happens in FooView. It seems from all I've read that the dialog box must be launched from the Activity class using showDialog(). I can't seem to find any example showing how to launch a dialog from a View.

So I'm wondering how this can be accomplished. Do I need to send an Intent from FooView to the Activity? Or register a callback from the Activity that is called by FooView?

Thanks in advance.

+1  A: 

You can register your callback in your Activity and call back to a method in the activity.

For example:

public class MyActivity extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    FooView foo = (FooView) findViewById(R.id.foo); 
    foo.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            launchDialog();
        }
    });
}





      private void launchDialog() {
    // do it here

        }
}
RickNotFred
That's what I suspected was necessary.
tronman