📜 ⬆️ ⬇️

We write the application for Android on Ruby (Ruboto)

I somehow had an idea (or a calculation on mobile platforms) to write applications for android on my favorite ruby. And then I remembered the project that was mentioned several times in the RubyNoName podcast, the actual discussion will be about Ruboto .


Well, let's get started. For programming, we need:
  1. JDK
  2. Android SDK
  3. Juby
  4. Gem ruboto

I will not begin to paint the installation, I will give a link to sufficiently detailed documentation.
After everything is installed, we generate the basis of our application.
ruboto gen app --package ru.nitrodev.demo_app 

Go to the demo_app directory and admire the carefully prepared application structure. In the src folder is already our first activation, and we’ll move on to it.
As an example I will sort out such basic things as:
  1. Interface creation
  2. Play audio using MediaPlayer
  3. Create Activate
  4. Determining the coordinates of the touch screen
  5. Work with Intent (page opening, dialing)
  6. Menu creation
Let's go in order.

Interface creation


In any application, we need an interface, we can use the standard way to create an interface using an XML resource, but then we lose all the flexibility of ruby. Therefore, the interface will be described in the code, for this we can call the Android API, or use ruboto/widget.rb which is a wrapper over the API and provides a simple DSL to create the interface.
Consider first the first method on the example of creating a LinearLayout containing one text field and 4 buttons.
First we import the necessary interface components.
 java_import "android.widget.Button" java_import "android.widget.TextView" java_import "android.widget.LinearLayout" 

We describe the interface itself
  ll = LinearLayout.new(@ruboto_java_instance) ll.orientation = LinearLayout::VERTICAL tv = TextView.new(@ruboto_java_instance) tv.text = " Java" ll.add_view tv #  TextView  LinearLayout params = tv.get_layout_params params.width = ViewGroup::LayoutParams::MATCH_PARENT btn1 = Button.new(@ruboto_java_instance) btn1.text = "Play" btn1.on_click_listener = proc{ Play } ll.add_view btn1 params = search_b.get_layout_params params.width = ViewGroup::LayoutParams::MATCH_PARENT btn2 = Button.new(@ruboto_java_instance) btn2.text = "Pause" btn2.on_click_listener = proc{ Pause } ll.add_view btn2 params = search_b.get_layout_params params.width = ViewGroup::LayoutParams::MATCH_PARENT btn3 = Button.new(@ruboto_java_instance) btn3.text = " " btn3.on_click_listener = proc{ twoActivity } ll.add_view btn3 params = search_b.get_layout_params params.width = ViewGroup::LayoutParams::MATCH_PARENT btn4 = Button.new(@ruboto_java_instance) btn4.text = " " btn4.on_click_listener = proc{ threeActivity } ll.add_view btn4 params = search_b.get_layout_params params.width = ViewGroup::LayoutParams::MATCH_PARENT setContentView ll 

You have to admit that this code is as cumbersome as java and hardly anyone will like it in the ruby ​​community. More elegant would be to use DSL Ruboto. Consider similar code using the Ruboto path.

 ruboto_import_widgets :Button, :LinearLayout, :TextView self.content_view = linear_layout :orientation => :vertical do text_view :text => ' Ruby -  ', :width => :match_parent button :text => 'Play', :width => :match_parent, :on_click_listener => proc { play } button :text => 'Pause', :width => :match_parent, :on_click_listener => proc { pause } button :text => ' ', :width => :match_parent, :on_click_listener => proc { twoActivity } button :text => ' ', :width => :match_parent, :on_click_listener => proc { threeActivity } end 

As you can see, the amount of code decreased by 4-5 times.
The result of this code

')

Play audio using MediaPlayer


To play sound, import MediaPlayer and AudioManager
 java_import "android.media.MediaPlayer" java_import "android.media.AudioManager" java_import "android.net.Uri" 

Then we get the file uri and create the MediaPlayer

 java_file = java.io.File.new("/mnt/sdcard/naive.mp3") uri = Uri.fromFile(java_file) @player = MediaPlayer.create(self, uri); 

We can work with our player with standard functions.
 @player.start @player.pause 

Create Activate


The remaining items of our manul will be created in separate activites.
The main way to create RubotoActivity is to use start_ruboto_activity .
start_ruboto_activity is the equivalent of Android startActivity in Ruboto. It has two required parameters: a string containing a global reference to the activity being created and a block that will be executed once when the activity is created.
Create a second activation

 start_ruboto_activity("$activity_two") do def on_create(bundle) super setTitle ' ' end end 

Determining the coordinates of the touch screen


Touch tracking is done using the on_touch_event function. This method is equivalent to the onTouchEvent android method.
We track the touch with one finger
 def on_touch_event(event) index = event.find_pointer_index(0) x = event.getX(index) y = event.getY(index) toast "Position: #{x}, #{y}" true end 

Work with Intent


To work with Intent, import the corresponding Android API
 java_import "android.content.Intent" 

Further work with Intent is done in the same way as java.

 intent = Intent.new(Intent::ACTION_CALL) intent.setData(Uri.parse("tel:5551234")) startActivity(intent) intent = Intent.new(Intent::ACTION_VIEW) intent.setData(Uri.parse("http://ruboto.org/")) startActivity(intent) 

Add appropriate permissions to AndroidManifest
 <uses-permission android:name="android.permission.CALL_PHONE" /> 

Menu creation


The standard callback on_create_options_menu used to work with the menu. Everything is simple: create menu items and hang the listener on them, when you click on toast message will be displayed with the name of the menu item.
 def on_create_options_menu(menu) m1 = menu.add('Action 1') m1.set_on_menu_item_click_listener do |menu_item| @text_view.text = menu_item_title toast menu_item.title true end m2 = menu.add('Action 2') m2.set_on_menu_item_click_listener do |menu_item| @text_view.text = menu_item.title toast menu_item.title true end true end 


That's all.
All this code can be viewed on github
Links for deeper immersion in Ruboto:
Wiki: github.com/ruboto/ruboto/wiki
Readme: github.com/ruboto/ruboto/blob/master/README.md
Excellent presentation with examples: jay.mcgavren.com/files/presentations/ruboto

Source: https://habr.com/ru/post/165071/


All Articles