Skip to content

Tutorial: dialogfragment

Scott Moyer edited this page Nov 28, 2013 · 10 revisions

Our goal

Display a dialog fragment in a Ruboto activity.

Prerequisites

This tutorial has been tested with the following setups:

ruboto RubotoCore Device Android Android SDK Tester
0.16.0.dev 0.5.6 Emulator 4.1.2 22.2.1 donv

Create your project

ruboto setup -y -t 15
ruboto gen app --package org.ruboto.example.dialog_fragment -t 15
cd dialog_fragment

Prepare the activity and add the dialog fragment

Change src/dialog_fragment_activity.rb to this

require 'ruboto/widget'

ruboto_import_widgets :LinearLayout, :TextView

class DialogFragmentActivity
  def onCreate(bundle)
    super
    set_title 'Dialog Fragment Example'

    self.content_view =
        linear_layout :orientation => :vertical, :gravity => :center do
          text_view :id => 42, :text => title, :text_size => 48.0,
                    :gravity => :center
        end
    ft = getFragmentManager.beginTransaction
    ft.addToBackStack(nil)
    ExampleDialogFragment.new.show(ft, 'example_dialog')
  end
end

class ExampleDialogFragment < android.app.DialogFragment
  def onCreate(bundle)
    super
    @some_var = 'Ruboto does fragments!'
  end

  def onCreateView(inflater, container, bundle)
    dialog.title = @some_var

    linear_layout :orientation => :vertical do
      linear_layout :gravity => :center, :layout => {:width => :fill_parent} do
        text_view :text => @some_var, :id => 43, :text_size => 40.0,
                  :gravity => :center
      end
    end
  end
end

Try it!

rake install start
Clone this wiki locally