Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get android:padding attribute programmatically

Tags:

android

From a view, how can I get the value of the android:padding attribute programmatically? I am currently using:

private static final String ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android";
private static final String ATTRIBUTE_PADDING = "padding";

public ActivityWrapperView(Context context, AttributeSet attrs) {
    super(context, attrs);
    int padding = attrs.getAttributeIntValue(ANDROID_NAMESPACE, ATTRIBUTE_PADDING, -1);
}

This returns -1, I have also tried using "android:padding" as the attribute name but -1 is still returned.

EDIT: My requirement: When an android:padding value is specified in the layout XML, the view will use this padding. If a padding is not specified it will use a default padding

like image 242
S-K' Avatar asked Aug 09 '13 06:08

S-K'


3 Answers

The easiest way was to use android.R.styleable, if it had had been available. The same way it uses for getting custom attributes. R.styleable is a class which contains an int arrays of attributes values. So you need to create your own int array, which contains int values of atributes you need.

public ActivityWrapperView(Context context, AttributeSet attrs) {
    super(context, attrs);

    //check attributes you need, for example all paddings
    int [] attributes = new int [] {android.R.attr.paddingLeft, android.R.attr.paddingTop, android.R.attr.paddingBottom, android.R.attr.paddingRight}

    //then obtain typed array
    TypedArray arr = context.obtainStyledAttributes(attrs, attributes);

    //and get values you need by indexes from your array attributes defined above
    int leftPadding = arr.getDimensionPixelOffset(0, -1);
    int topPadding = arr.getDimensionPixelOffset(1, -1);

    //You can check if attribute exists (in this examle checking paddingRight)
    int paddingRight = arr.hasValue(3) ? arr.getDimensionPixelOffset(3, -1) : myDefaultPaddingRight;


}

EDIT as per @Eselfar's comment:
Don't forget to release the TypedArray: arr.recycle() !

like image 75
cooperok Avatar answered Sep 18 '22 18:09

cooperok


You can just add the android:padding to your custom view's attributes.

<declare-styleable name="ActivityWrapperView">
    ...
    <attr name="android:padding" />
    ...
</declare-styleable>

And then, you can access the attribute just like your other attributes:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActivityWrapperView);
float padding = a.getDimension(R.styleable.ActivityWrapperView_android_padding, 0);
...
boolean hasPadding = a.hasValue(R.styleable.ActivityWrapperView_android_padding);
like image 40
Tao Wang Avatar answered Sep 16 '22 18:09

Tao Wang


You should look at the getPadding____() functions.

Size, padding and margins

...

To measure its dimensions, a view takes into account its padding. The padding is expressed in pixels for the left, top, right and bottom parts of the view. Padding can be used to offset the content of the view by a specific amount of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge. Padding can be set using the setPadding(int, int, int, int) or setPaddingRelative(int, int, int, int) method and queried by calling getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom(), getPaddingStart(), getPaddingEnd().

Even though a view can define a padding, it does not provide any support for margins. However, view groups provide such a support. Refer to ViewGroup and ViewGroup.MarginLayoutParams for further information.

like image 37
Emil Davtyan Avatar answered Sep 18 '22 18:09

Emil Davtyan