Registering a View for a Context Menu

The first step in implementing a context menu is registering a view for the context menu in an activity's onCreate() method. If you were to use the menu test harness introduced in this chapter, you could register the TextView for a context menu in that test harness by using the code in Listing 5-15. You will first find the TextView and then call registerForContextMenu on the activity using the TextView as an argument. This will set up the TextView for context menus.

Listing 5-15. Registering a TextView for a Context Menu ^Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

TextView tv = (TextView)this.findViewById(R.id.textViewId); registerForContextMenu(this.getTextView())j

0 0

Post a comment