Skip to content

Tutorial: Add a menu

Stoneynine edited this page Nov 25, 2015 · 11 revisions

Goal

Add a menu to a newly generated app.

Prerequisites

You should have completed the Getting started with Ruboto tutorial. You can use an android device or the emulator to run your app.

This tutorial has been tested on the following configurations

ruboto jruby-jars Device api-level Tester
0.13.0 1.7.4 Samsung Galaxy S3 16 donV
0.10.0 1.7.1 Samsung Galaxy S3 15
0.10.0 1.7.1 Emulator 15
0.10.0 1.7.1 Emulator 10
0.10.1 Lenovo P700 15 rystraum

Create the app and run it

ruboto gen app --package org.ruboto.examples.menu_example
cd menu_example
rake install start

You should now see the familiar “What hath Matz wrought?”.

A fresh app

Add the menu

Add a callback for opening the menu. Edit the src/menu_example_activity.rb, and add the following method:

def onCreateOptionsMenu(menu)
  menu.add('Action 1').set_on_menu_item_click_listener do |menu_item|
    @text_view.text = menu_item.title
    toast menu_item.title
    true # Prevent other listeners from executing.
  end

  mi = menu.add('Action 2')
  mi.set_icon $package.R::drawable::get_ruboto_core
  mi.set_on_menu_item_click_listener do |menu_item|
    @text_view.text = menu_item.title
    toast menu_item.title
    true # Prevent other listeners from executing.
  end
  true # Display the menu.
end

Redeploy your app

rake install start

After your app restarts, you should be able to press the menu button to display a menu. Selecting any of the menu items should pop up a toast with the title of the menu item.

Clone this wiki locally