Setting the padding of a view

23 views
Skip to first unread message

KC Erb

unread,
Jun 25, 2014, 12:00:31 PM6/25/14
to rub...@googlegroups.com
Hi all,

I'm trying to set the top padding of my layout:

   self.content_view =
      linear_layout orientation: :vertical, padding_top: 48  do

        text_view text: 'What hath Matz wrought?!',
                  layout: {width: :match_parent},
                  gravity: :center, text_size: 48.0
      end

Reading the API it seems that LinearLayout should inherit the padding attribute from android.view.View. But when I try to set it, it fails.

Suggestions?

-KC

Scott Moyer

unread,
Jun 26, 2014, 3:20:01 PM6/26/14
to rub...@googlegroups.com

My understanding of the API is that there is no method for setting just one of the padding components (there is an XML property, but no method). You have to use an array for our hash element :

padding: [5,48,5,5]

If you want to leave the other padding in tact, you can get the values individually,  and use them in the call above.

--
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/d/optout.

KC Erb

unread,
Jun 28, 2014, 12:48:58 PM6/28/14
to rub...@googlegroups.com
Hi Scott,

In the API I see now a setPadding method for a LinearLayout working as you described. Thanks for pointing that out!

I originally thought that the hash we pass to a widget sets its XML attributes. Are you saying that the hash corresponds to methods on the widget?

For example, when I say:
self.content_view =
      linear_layout orientation: :vertical, . . .

I'm making a call to the LinearLayout's setOrientation method, not setting it's orientation attribute?

  . . . Ah, I see now that many of the XML attributes have a 'Related Method' association. So if I want to set an XML attribute I need to find the method that set's it and call that in the Hash.


Is that right?

-KC

KC Erb

unread,
Jun 28, 2014, 5:37:52 PM6/28/14
to rub...@googlegroups.com
Hi again,

I'm having a problem with this. Setting the padding as you suggested is causing my example app to fail. I tried using floats instead but still no luck. Any reason why the following wouldn't work in your opinion?

------------------------------------------------------------
require 'ruboto/widget'
 
ruboto_import_widgets :LinearLayout, :TextView

class TestActivity
  def onCreate(bundle)
    super
   self.content_view =
      linear_layout orientation: :vertical, padding: [5, 48, 5, 5]  do

        text_view text: 'What hath Matz wrought?!',
                  layout: {width: :match_parent},
                  gravity: :center, text_size: 48.0
      end  
  end
end
------------------------------------------

(By the way this code works fine without the padding bit.)


I also tried using a dimension value as the API suggests (http://developer.android.com/reference/android/view/View.html#attr_android:paddingTop), based on what I found in this issue (https://github.com/ruboto/ruboto/issues/159)

------------------------------------------
require 'ruboto/widget'
 
ruboto_import_widgets :LinearLayout, :TextView
java_import 'android.util.TypedValue'

class ActionBarActivity
  def onCreate(bundle)
    super

    dp = TypedValue.applyDimension TypedValue::COMPLEX_UNIT_DIP, 50, getResources.getDisplayMetrics
    
    self.content_view =
      linear_layout orientation: :vertical, padding: [dp, dp, dp, dp]  do

        text_view text: 'What hath Matz wrought?!',
                  layout: {width: :match_parent},
                  gravity: :center, text_size: 48.0
      end  
  end
end
------------------------------------------------------------

Any suggestions?

Suller Andras

unread,
Jun 29, 2014, 9:09:46 AM6/29/14
to rub...@googlegroups.com
On Sun, Jun 29, 2014 at 5:37 AM, KC Erb <iamk...@gmail.com> wrote:
> Hi again,
>
> I'm having a problem with this. Setting the padding as you suggested is
> causing my example app to fail. I tried using floats instead but still no
> luck. Any reason why the following wouldn't work in your opinion?

Since you said it "failed", was there any exception? What is the error
message? That might help resolving the problem.

Regards,
Andras

KC Erb

unread,
Jun 29, 2014, 11:12:53 AM6/29/14
to rub...@googlegroups.com

That's a great question! It's actually a major problem I'm still having; I don't know how to get error messages :(

I've tried using IRB, but I can't get the example scripts to run from the server (they run on my device just fine).

So what is the best way to get an error message out?

--
You received this message because you are subscribed to a topic in the Google Groups "Ruboto (JRuby on Android)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruboto/PYtcvo5s_jo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruboto+un...@googlegroups.com.

Scott Moyer

unread,
Jun 30, 2014, 7:55:06 AM6/30/14
to rub...@googlegroups.com
I've been out of communication, but I should be able to look into this more now.

I put it into IRB and got the error that there was no padding method. I need to look into this more to see if we broke something along the way.

I'll send more info later.


--
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.

Scott

unread,
Jun 30, 2014, 10:02:54 AM6/30/14
to rub...@googlegroups.com
So, there is definitely a problem, but I can't identify it yet. The code looks like it should correctly try setPadding, but it's trying padding. I need to do more investigation into the internals of widget.rb.

In the interim, you can use set_padding:  [5, 48, 5, 5]

Scott

unread,
Jun 30, 2014, 10:06:31 AM6/30/14
to rub...@googlegroups.com

On Saturday, June 28, 2014 12:48:58 PM UTC-4, KC Erb wrote:
Hi Scott,

In the API I see now a setPadding method for a LinearLayout working as you described. Thanks for pointing that out!

I originally thought that the hash we pass to a widget sets its XML attributes. Are you saying that the hash corresponds to methods on the widget?

For example, when I say:
self.content_view =
      linear_layout orientation: :vertical, . . .

I'm making a call to the LinearLayout's setOrientation method, not setting it's orientation attribute?

  . . . Ah, I see now that many of the XML attributes have a 'Related Method' association. So if I want to set an XML attribute I need to find the method that set's it and call that in the Hash.


Is that right?

Yes, that's right. In many cases it's one-to-one, but in some you need to do more work to figure out the best way to set the view property. 

donV

unread,
Jul 28, 2014, 11:41:59 AM7/28/14
to rub...@googlegroups.com
Hi Scott and KC!

I found that we did not try the setPadding variant for attributes not confirming to the JavaBeans standard like setPadding(l,t,r,b) <= uses four parameters.  JavaBean attribute setters may only have one parameter.


I fixed it on master, so you can test it there, or just copy "assets/src/ruboto/widget.rb to your "src/ruboto" directory.  It will be released in the next version of Ruboto, due in August.

Uwe Kubosch

unread,
Oct 13, 2014, 3:33:33 PM10/13/14
to rub...@googlegroups.com
I finally released this change now in Ruboto 1.2.0.
> --
> 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/d/optout.

--
Uwe Kubosch
u...@kubosch.no
http://kubosch.no/




Reply all
Reply to author
Forward
0 new messages