James Scott
James Scott

Reputation: 1230

Getting ObtainStyledAttributes to work properly in xamarin for Android

I'm building a custom control in Xamarin.Android and as part of the initialization of that control I'm want to read the specified height for the control.

I'd like to use ObtainStyledAttributes because it gives me a way to convert string dimensions to dimensions easily.

So I call:

 context.ObtainStyledAttributes(attrs, new int [] { problemhere })

The issue I'm having is I don't know how to specify the constant for layout_height (this is the parameter I am looking to extract). When I looked into the Xamarin documentation it states that there is a Resource.Attribute.LayoutWidth const defined, but I can't reference it in my source code.

I know I can also use getAttributeValue() to get as a string, but then it doesn't convert.

Any ideas?

Upvotes: 2

Views: 310

Answers (1)

SushiHangover
SushiHangover

Reputation: 74174

You are looking for the Android OS level resources:

int[] attrsArray = { 
    Android.Resource.Attribute.LayoutWidth,
    Android.Resource.Attribute.LayoutHeight 
};
var typedArray = context.ObtainStyledAttributes( attrsArray);
var layout_width = typedArray.GetDimensionPixelSize(0, ViewGroup.LayoutParams.WrapContent);
var layout_height = typedArray.GetDimensionPixelSize(1, ViewGroup.LayoutParams.WrapContent);

Upvotes: 2

Related Questions