četvrtak, 14. kolovoza 2014.

How to create and add menu item - android

First go res --> menu --> and open xml file which contains menu. Usually, it is called menu.xml. As shown on picture:



Edit menu.xml file so it contains new menu entries. It can look like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item

        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/menuAction_unesiNoviUlaganje"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/menu_unesiNovoUlaganje"/>


</menu>


Afterwards, go to MainActivity.java and enter new method onCreateOptionsMenu which will create menu according to xml file menu.xml:




Again in MainActivity.java, enter new method onOptionsItemSelected which will handle menu calls:



And that's it. Bear in mind, that first case action_settings is deafault value which is present, and you need to add your own and process is it as you like. Here, in this example, by pressing new menu item, a new activity is being called.

Nema komentara:

Objavi komentar