Class: AndroidQuery
- Inherits:
-
Object
- Object
- AndroidQuery
- Defined in:
- lib/android_query/android_query.rb
Instance Attribute Summary collapse
-
#activity ⇒ Object
Returns the value of attribute activity.
Instance Method Summary collapse
- #button(layout, options = {}) ⇒ Object
- #create_layout_with_params(layout, options) ⇒ Object
- #create_view_layout_params(view, layout, options) ⇒ Object
- #edit_text(layout, options = {}) ⇒ Object
- #find(view_id) ⇒ Object
- #get_height(options, layout) ⇒ Object
- #get_layout(layout, options) ⇒ Object
- #get_orientation(options, layout) ⇒ Object
- #get_width(options, layout) ⇒ Object
-
#initialize(source) ⇒ AndroidQuery
constructor
A new instance of AndroidQuery.
- #layout(layout_sym, options = {}, &block) ⇒ Object
- #merge_options(options) ⇒ Object
- #set_content(layout, params = {}) ⇒ Object
- #text_view(layout, options = {}) ⇒ Object
- #toast(text, options = {}) ⇒ Object
Constructor Details
#initialize(source) ⇒ AndroidQuery
Returns a new instance of AndroidQuery.
4 5 6 |
# File 'lib/android_query/android_query.rb', line 4 def initialize(source) self.activity = source end |
Instance Attribute Details
#activity ⇒ Object
Returns the value of attribute activity.
2 3 4 |
# File 'lib/android_query/android_query.rb', line 2 def activity @activity end |
Instance Method Details
#button(layout, options = {}) ⇒ Object
34 35 36 37 |
# File 'lib/android_query/android_query.rb', line 34 def (layout, = {}) btn = Android::Widget::Button.new(self.activity) create_view_layout_params(btn, layout, ) end |
#create_layout_with_params(layout, options) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/android_query/android_query.rb', line 110 def create_layout_with_params(layout, ) = () width = get_width(, layout) height = get_height(, layout) orientation = get_orientation(, layout) my_layout = layout[:layout_class].new(self.activity) layout_params = layout[:params_class].new(width, height) layout_params.weight = [:weight] if layout[:layout_sym] == :linear my_layout.setLayoutParams(layout_params) my_layout.setOrientation(orientation) if layout[:layout_sym] == :linear my_layout.setWeightSum([:weight_sum]) if layout[:layout_sym] == :linear my_layout end |
#create_view_layout_params(view, layout, options) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/android_query/android_query.rb', line 124 def create_view_layout_params(view, layout, ) = () width = get_width(, layout) height = get_height(, layout) layout_params = layout[:params_class].new(width, height) layout_params.weight = [:weight] view.setText([:text]) unless [:text].nil? view.setLayoutParams(layout_params) view.onClickListener = AQClickListener.new(self.activity, ) if [:click] view.setId([:id]) if [:id] layout[:layout].addView(view) view end |
#edit_text(layout, options = {}) ⇒ Object
29 30 31 32 |
# File 'lib/android_query/android_query.rb', line 29 def edit_text(layout, = {}) et = Android::Widget::EditText.new(self.activity) create_view_layout_params(et, layout, ) end |
#find(view_id) ⇒ Object
138 139 140 |
# File 'lib/android_query/android_query.rb', line 138 def find(view_id) self.activity.findViewById(view_id) end |
#get_height(options, layout) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/android_query/android_query.rb', line 88 def get_height(, layout) case [:h] when :mp, :match_parent layout[:params_class]::MATCH_PARENT when :wc, :wrap_content layout[:params_class]::WRAP_CONTENT else [:h] end end |
#get_layout(layout, options) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/android_query/android_query.rb', line 43 def get_layout(layout, ) = () layout_and_params = case layout when :linear [Android::Widget::LinearLayout, Android::Widget::LinearLayout::LayoutParams] when :relative [Android::Widget::RelativeLayout, Android::Widget::RelativeLayout::LayoutParams] when :absolute [Android::Widget::AbsoluteLayout, Android::Widget::AbsoluteLayout::LayoutParams] else layout end { layout: nil, layout_class: layout_and_params[0], params_class: layout_and_params[1], layout_sym: layout, parent: [:parent], } end |
#get_orientation(options, layout) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/android_query/android_query.rb', line 99 def get_orientation(, layout) case [:orientation] when :vertical, :v layout[:layout_class]::VERTICAL when :horizontal, :h layout[:layout_class]::HORIZONTAL else raise 'Please set either :horizontal or :vertical for the orientation' end end |
#get_width(options, layout) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/android_query/android_query.rb', line 77 def get_width(, layout) case [:w] when :mp, :match_parent layout[:params_class]::MATCH_PARENT when :wc, :wrap_content layout[:params_class]::WRAP_CONTENT else [:w] end end |
#layout(layout_sym, options = {}, &block) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/android_query/android_query.rb', line 8 def layout(layout_sym, = {}, &block) layout_info = get_layout(layout_sym, ) my_layout = create_layout_with_params(layout_info, ) block_layout = { layout: my_layout, layout_class: layout_info[:layout_class], params_class: layout_info[:params_class], layout_sym: layout_sym, parent: layout_info[:parent], } block.call(block_layout) block_layout[:parent][:layout].addView(my_layout) if block_layout[:parent] set_content(block_layout) if block_layout[:parent].nil? block_layout end |
#merge_options(options) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/android_query/android_query.rb', line 64 def () { w: :mp, h: :wc, orientation: :vertical, weight: 0, weight_sum: 0, parent: nil, click: nil, id: nil, }.merge() end |
#set_content(layout, params = {}) ⇒ Object
39 40 41 |
# File 'lib/android_query/android_query.rb', line 39 def set_content(layout, params = {}) self.activity.setContentView(layout[:layout]) end |
#text_view(layout, options = {}) ⇒ Object
24 25 26 27 |
# File 'lib/android_query/android_query.rb', line 24 def text_view(layout, = {}) tv = Android::Widget::TextView.new(self.activity) create_view_layout_params(tv, layout, ) end |
#toast(text, options = {}) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/android_query/android_query.rb', line 142 def toast(text, = {}) = { gravity: :bottom, length: :short }.merge() = { top: Android::View::Gravity::TOP, left: Android::View::Gravity::LEFT, right: Android::View::Gravity::RIGHT, bottom: Android::View::Gravity::BOTTOM, center: Android::View::Gravity::CENTER, } = { short: Android::Widget::Toast::LENGTH_SHORT, long: Android::Widget::Toast::LENGTH_LONG, } the_toast = Android::Widget::Toast.makeText(self.activity, text, [[:length]]) the_toast.setGravity([[:gravity]], 0, 0) the_toast.show end |