Tip:
Highlight text to annotate it
X
Hello everyone, this is a lesson 15, in which we
create a context menu.
The context menu
called in Android by holding down
some display components. Usually it
used in the lists. When the screen
a list of similar items
(E.g. letter Mailbox), and to
perform an action on one of these objects,
we call the context menu for it. But
because lists, we still do not pass, do
example simpler and will call context
menu for the TextView. Let's create a project named P0151_ContextMenu.
Open the main.xml and there are two draw
TextView. For the first TextView we do
context menu, with which we
change the text color. For the second - will
change the text size.
Principle of creation
context menu is similar to the creation of customary
menu. But there are differences.
The method of creating
onCreateContextMenu called every time
before showing the menu. It is passed to the input:
-ContextMenu, in which we will be adding
items - View - screen element for
whom
Call the popup menu -
ContextMenu.ContextMenuInfo - contains
additional information, when the context menu
called for the list item. As long as we do not
use, but as we study lists
see that a useful thing.
Processing Method
onContextItemSelected similar method
onOptionsItemSelected to the regular menu. On
input is passed MenuItem - menu item
that was hit.
We also need
the third method registerForContextMenu. The input
View and it is passed, this means that for
View of the need to create a context
menu. Failure to perform this method,
context menu for the View is not created
will.
Let's write code, open
MainActivity.java. We describe and find the TextView and
we point out that it is necessary for them to create
the context menu.
We now describe the creation
the contextual menu. Use constants for
storing menu ID.
And create
Please
note that we define ID View, for
which caused the context menu and
Depending on this definition is created
menu. That is, if the context menu is called for
tvColor, then we create a menu with a list of
colors, and if tvSize - with dimensions
font.
As we used the ID points
constants. Grouping and sorting is not
use, so we use the zeros in
as the respective parameters.
Can all
maintain and run. A long press on
TextView context menu should appear.
But hitting on them does nothing, because we do not
not prescribed in the method of processing
onContextItemSelected. Let us prescribe:
In
this method, we determine by ID, which item
the menu is clicked. And the appropriate
action: change the text color or for tvColor
font size for tvSize. Save,
run and check that the context menus
now respond to the pressing and doing what
required of them.
To expand the horizons I
I would like to say something on the subject.
It may sound complicated yet so
that if all of a sudden will not understand anything
okay. So, thinking out loud.
We used
method registerForContextMenu (View view) for
enable the context menu for a particular
View. This method belongs to the class Activity.
I looked at the source code of this method, there
the following is written:
Remembering our lesson
handlers and look HELP method
setOnCreateContextMenuListener
(View.OnCreateContextMenuListener l).
It turns out that the View as a handler
create a context menu using the object
this. In this case, the code Activity,
mean this - this Activity is. That is, when
View wants to show the context menu, it
refers to the handler (Activity), and he has
performs his method onCreateContextMenu.
That is, the same principle as in the conventional
Pressing (Click).
And the line in MainActivity.java
This line is equivalent to:
Generally we
can create your own object that implements
interface and View.OnCreateContextMenuListener
use it instead as an Activity
create a context menu handler.
Remember that for the context menu, you also
can use XML-method which has been
reviewed at the end of the last lesson. Try
do the same lesson, but with the use of
XML-menu.
If you had any problems, I
I remind you that the full code lesson you can
view a text version of the tutorial. Link
it is usually in the description.
And
the next lesson, we will try to draw the screen
software and not through Layout file.