Activity and Intents

85 views
Skip to first unread message

wil...@planobe.com.br

unread,
Jun 16, 2013, 3:17:30 PM6/16/13
to rub...@googlegroups.com
Hi guys!

Sorry for bother you with a simple question, but, I'm trying to learn how to start another activity with Ruboto. I followed a thousand tutorials like: https://github.com/ruboto/ruboto/wiki/Using-Activities-and-Intents, but I really couldn't understand some things. My goal is simple, I just want to catch a message from EditText(pressing a Button) and show in a TextView.

Can you make a simple tutorial step by step? It will help a lot!
Thanks!

Scott Moyer

unread,
Jun 16, 2013, 11:53:16 PM6/16/13
to rub...@googlegroups.com

Hi William,

While you could open an Activity to get a piece of information, it is really more appropriate to us an AlertDialog for that. Here's an example where we do that to get a line number (to jump to) in the demo-ruboto-irb script in Ruboto IRB:

et = edit_text(:hint => "Line number", :input_type => android.text.InputType::TYPE_CLASS_NUMBER)
     
goto = proc do
    @edit_script.requestFocus
    line = et.text.to_s.to_i - 1
    max_line = @edit_script.getLineCount
    line = max_line if line > max_line
    line = 1 if line < 1
     @edit_script.setSelection @edit_script.getLayout.getLineStart(line)
end

dialog = AlertDialog::Builder.new(@ruboto_java_instance).setTitle("Go to").setView(et).setPositiveButton("Go", goto).create
dialog.window.soft_input_mode = android.view.WindowManager::LayoutParams::SOFT_INPUT_STATE_ALWAYS_VISIBLE
dialog.show

The et (EditText) is the view for getting the information. You'll need to require 'ruboto/widget' and ruboto_import_widgets :EditText.

The goto proc will be called when the user presses the "Go" button.

--
You received this message because you are subscribed to the Google Groups "Ruboto (JRuby on Android)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruboto+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gerard Fowley

unread,
Jun 16, 2013, 11:57:26 PM6/16/13
to ruboto
William,

Did you have a look at the test code linked to in that wiki article ?


I only say that because that's what made Activities and Intents with jruby click for me.

Also, It seems that an AlertDialog may be more appropriate than an Activity for your goal of accepting a single input from the user.

-gf-

Gerard Fowley
CISSP
Information Security Consultant
Principal @ Iqeo LLC
gerard...@iqeo.net
508-769-9617
http://iqeo.net




--

wil...@planobe.com.br

unread,
Jun 17, 2013, 8:43:19 PM6/17/13
to rub...@googlegroups.com
Hey Scott!

Thanks for your help! But all of the code will look like this?

require 'ruboto/widget'
ruboto_import_widgets :EditText

class Walk_mobile_androidActivity
  def onCreate(bundle)

    set_contetnt_view = inear_layout :orientation => :vertical do


      et = edit_text(:hint => "Line number", :input_type => android.text.InputType::TYPE_CLASS_NUMBER)

      goto = proc do
        @edit_script.requestFocus
        line = et.text.to_s.to_i - 1
        max_line = @edit_script.getLineCount
        line = max_line if line > max_line
        line = 1 if line < 1
        @edit_script.setSelection @edit_script.getLayout.getLineStart(line)
      end

      dialog = AlertDialog::Builder.new(@ruboto_java_instance).setTitle("Go to").setView(et).setPositiveButton("Go", goto).create
      dialog.window.soft_input_mode = android.view.WindowManager::LayoutParams::SOFT_INPUT_STATE_ALWAYS_VISIBLE
      dialog.show
    end
  end
end

This code works on the Ruboto-core? Because is crashing in my emulator. One more thing, Can I do that in the same way that is explained on android documentation? http://developer.android.com/training/basics/firstapp/starting-activity.html#

oh, I forgot to say, I'm new on Ruboto development :D

Scott

unread,
Jun 18, 2013, 12:50:39 AM6/18/13
to rub...@googlegroups.com
No, the dialog should not be inside the Activity's content. Here's a shot at what you're trying to do:

require 'ruboto/activity'
require 'ruboto/widget'

ruboto_import_widgets :EditText, :LinearLayout, :TextView, :Button

class Walk_mobile_androidActivity
  def onCreate(bundle)
    self.contetnt_view = linear_layout(:orientation => :vertical) do
      button :text => "Get Info", :on_click_listener => (proc{show_dialog})
      @my_text = text_view
    end
  end

  def show_dialog
      et = edit_text(:hint => "Enter Your Info")

      get_info = proc do
        @my_text.text = et.text
      end

      dialog = AlertDialog::Builder.new(@ruboto_java_instance).setTitle("Your Info").setView(et).setPositiveButton("Go", get_info).create

      dialog.window.soft_input_mode = android.view.WindowManager::LayoutParams::SOFT_INPUT_STATE_ALWAYS_VISIBLE
      dialog.show
  end
end

a a

unread,
Aug 1, 2014, 3:43:35 AM8/1/14
to rub...@googlegroups.com

No, the dialog should not be inside the Activity's content.

Why the dialog can't be inside of MyActivity?
I think this is the reason why I get a NullPointerException at:

AlertDialog::Builder.new(@ruboto_java_instance)
...

Can someone clarify In which scope does @ruboto_java_instance exist ?
It has to exist inside of of MyActivity because how could you otherwise do this ->

search = EditText.new(@ruboto_java_instance)

...inside of MyActivity's onCreate method ?

Since it seems to be available inside of onCreate method,
I also tried - from there - to copy @ruboto_java_instance into a
$global_var.

$rji_glob = @ruboto_java_instance

And then again I tried to pass it to "AlertDialog::Builder.new($rji_glob)" (inside of Activity)
but still I see a NullPointerException...

Does this make sense?
Also why not allow to use @ruboto_java_instance from inside the Activity when in java the same construct is working? (Check here: http://www.chupamobile.com/tutorial-android/android-beginner-tutorial-part-43-alertdialog-custom-layout-312)

The reason I am giving attention to this details is because when I am using Ruboto and learning Android API's I found myself many times looking for java examples and then trying to translate them into some nice Ruby code.. But as I see a profound diversity in the code structure, I feel really discouraged because then this difference puts me in a position where I have to learn the tedious bits of the two different environments, which completely wastes the original purpose of doing things in simpler and nicer way.

I hope that in the near future Ruboto will fix all discrepancies with Android Java programming.
Since I am here, are there any other big differences that I should be aware of?
Thanks for helping.

a a

unread,
Aug 1, 2014, 4:31:34 AM8/1/14
to rub...@googlegroups.com

... Here's a shot at what you're trying to do:

I've also tried copying this example exactly as it is and I'm still getting the null pointer exception... what....?

a a

unread,
Aug 1, 2014, 4:43:10 AM8/1/14
to rub...@googlegroups.com
Wow, I spent nearly a day on this. Not trying the most evident solution because it was told to be wrong........
With "self" it works... damnit..

Scott

unread,
Aug 2, 2014, 2:22:16 AM8/2/14
to rub...@googlegroups.com
Let me say that there are a few errors with my code above. I think I was just trying to give an example of the structure. I wasn't actually testing the code. Here are the errors:

1) I Didn't call super in onCreate
2) I misspelled content in self.content_view
3) I didn't import AlertDialog or give its full name

This code works:

require 'ruboto/activity'
require 'ruboto/widget'

ruboto_import_widgets :EditText, :LinearLayout, :TextView, :Button

class Walk_mobile_androidActivity
  def onCreate(bundle)
    super
    self.content_view = linear_layout(:orientation => :vertical) do
      button :text => "Get Info", :on_click_listener => (proc{show_dialog})
      @my_text = text_view
    end
  end

  def show_dialog
      et = edit_text(:hint => "Enter Your Info")

      get_info = proc do
        @my_text.text = et.text
      end

      dialog = android.app.AlertDialog::Builder.new(@ruboto_java_instance).setTitle("Your Info").setView(et).setPositiveButton("Go", get_info).create

      dialog.window.soft_input_mode = android.view.WindowManager::LayoutParams::SOFT_INPUT_STATE_ALWAYS_VISIBLE
      dialog.show
  end
end

Scott

unread,
Aug 2, 2014, 2:49:30 AM8/2/14
to rub...@googlegroups.com
Yes, it is confusing. I have a tendency to use Ruboto IRB to debug. I don't think I fully understood (until today) that places in Ruboto IRB that require @ruboto_java_instance just use self in regular ruboto code (ruboto gen app). I think Uwe understands this subtle difference best. Uwe, help!

Scott

unread,
Aug 2, 2014, 2:54:35 AM8/2/14
to rub...@googlegroups.com
Ok, for the code above in a ruboto gen app:

1) Create the app

    ruboto gen app --package org.ruboto.example.my_test

2) Change the code in src/my_test_activity.rb

    require 'ruboto/activity'
    require 'ruboto/widget'
    
    ruboto_import_widgets :EditText, :LinearLayout, :TextView, :Button
    
    class MyTestActivity
      def onCreate(bundle)
        super
        self.content_view = linear_layout(:orientation => :vertical) do
          button :text => "Get Info", :on_click_listener => (proc{show_dialog})
          @my_text = text_view
        end
      end
    
      def show_dialog
          et = edit_text(:hint => "Enter Your Info")
    
          get_info = proc do
            @my_text.text = et.text
          end
    
          dialog = android.app.AlertDialog::Builder.new(self).setTitle("Your Info").setView(et).setPositiveButton("Go", get_info).create

donV

unread,
Oct 13, 2014, 3:42:07 PM10/13/14
to rub...@googlegroups.com
Hi a a and Scott!

I'm a bit late to the show (been working on the 1.2.0 release), but did this work out for you?
Reply all
Reply to author
Forward
0 new messages