43154

Setting layout properties in code at runtime

Question:

I am having trouble finding out how to set layout properties in my code. My controls are being generated at runtime so I can't set the layout in my xml.

I would like to be able to set properties such as

android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_weight="1"

However I can't find any documentation or examples on how to do this in code. Is it possible with the current version of Mono for Android?

Answer1:

<a href="http://lists.ximian.com/mailman/private/monodroid/2011-April/004556.html" rel="nofollow">Relevant thread on the Mono for Android mailing list.</a>

Many of the constructors take an <a href="http://docs.mono-android.net/index.aspx?link=T%3aAndroid.Util.IAttributeSet" rel="nofollow">IAttributeSet</a> instance, so (worst case) you could always provide the XML custom attributes through that parameter when invoking the e.g. <a href="http://docs.mono-android.net/index.aspx?link=C%3aAndroid.Widget.RelativeLayout(Android.Content.Context%2cAndroid.Util.IAttributeSet)" rel="nofollow">RelativeLayout(Context, IAttributeSet) constructor</a>.

Resource attributes are handled specifically in Java code, and thus can potentially vary from one class to another. For example, the <a href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/RelativeLayout.java;h=a47359f8a6a4c7c4e1b9a2cbd16e8e929b3bd01b;hb=HEAD#l172" rel="nofollow">RelativeLayout constructor implementation</a>.

Because of this, attributes can (and will be) specific to a given type. For example, as best as I can tell from quickly perusing the Android source, it's not valid for a type to have both android:layout_alignParentBottom and android:layout_weight attributes, as <a href="http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_alignParentBottom" rel="nofollow">android:layout_alignParentBottom</a> appears to be specific to the RelativeLayout type, while <a href="http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_weight" rel="nofollow">android:layout_weight</a> is specific to <a href="http://docs.mono-android.net/index.aspx?link=T%3aAndroid.Widget.LinearLayout" rel="nofollow">LinearLayout</a>, and there is no inheritance relationship between RelativeLayout and LinearLayout.

That said, to programmatically assign the android:layout_alignParentBottom property, it looks like you'd want to do:

// Get a RelativeLayout.LayoutParams instance // Option 1, if you have an existing RelativeLayout instance already: var p = (RelativeLayout.LayoutParams) layout.LayoutParameters; // Option 2: if you don't. var p = new RelativeLayout.LayoutParams (context, null); // Enable layout_alignParentBottom: p.AddRule ((int) LayoutRules.AlignParentBottom);

This uses the <a href="http://docs.mono-android.net/index.aspx?link=M%3aAndroid.Widget.RelativeLayout.LayoutParams.AddRule(System.Int32)" rel="nofollow">RelativeLayout.LayoutParams.AddRule</a> method to enable the layout option. The int cast is necessary because we didn't realize that AddRule() should take a LayoutRules enum; oops.

To programmatically assign the android:layout_alignParentRight property:

p.AddRule ((int) LayoutRules.AlignParentRight);

As noted above, it appears that <a href="http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_weight" rel="nofollow">android:layout_weight</a> is specific to LinearLayout, so we can't use RelativeLayout.LayoutParams to set this. Instead, we need to use LinearLayout.LayoutParams to set the <a href="http://docs.mono-android.net/index.aspx?link=P%3aAndroid.Widget.LinearLayout.LayoutParams.Weight" rel="nofollow">LinearLayout.LayoutParams.Weight</a> property:

// Just like before, but get a LinearLayout.LayoutParams instance // Option 1, if you have an existing LinearLayout instance already: var p = (LinearLayout.LayoutParams) layout.LayoutParameters; // Option 2: if you don't. var p = new LinearLayout.LayoutParams (context, null); // Enable layout_weight: p.Weight = 1.0f;

Recommend

  • Best way to determine if a user has viewed data
  • What should be the return value of a custom function addEdge in a new class based on BGL?
  • PHP and MySQL: Comparing month and day with dynamic year
  • declaring device constant in terms of another constant
  • Why is new Number(8) not exactly equal to 8?
  • Zend Framework 2 - Building a simple form with Validators
  • php notice undefined index http_host in $GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST'
  • VirtualDocumentRoot handling non-existent folders
  • Android NFC read Tags issue. Activity starts each time on data received
  • How to best manage SMTP clients
  • Validation fired but Red Border does not appear with User Control in Silverlight 4
  • custom string delimiters stringtemplate-4
  • auth.provider is not set to 'password' when user signs-in with email and password
  • Getting unread count in Sent Folder using Google Apps Script - GMail
  • Why must we declare a variable name when adding a method to a struct in Golang?
  • Python ImageIO Gif Set Delay Between Frames
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Textfile Structure (tables)
  • Email verification using google app script and google forms
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Disable Enter in editText android
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Cannot resolve symbol 'MyApi'
  • Highlight one bar in a series in highcharts?
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • req.body is undefined - nodejs
  • Spray.io: When (not) to use non-blocking route handling?
  • How to make Safari send if-modified-since header?
  • Release, debug version and Authorization Google?
  • Calling of Constructors in a Java
  • How to pass list parameters for each object using Spring MVC?
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • PHP: When would you need the self:: keyword?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • sending mail using smtp is too slow
  • JaxB to read class hierarchy
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?