Get android:padding attribute programmatically -


from view, how can value of android:padding attribute programmatically? 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, have tried using "android:padding" attribute name -1 still returned.

edit: requirement: when android:padding value specified in layout xml, view use padding. if padding not specified use default padding

the easiest way use android.r.styleable, if had had been available. same way uses getting custom attributes. r.styleable class contains int arrays of attributes values. need create own int array, contains int values of atributes need.

public activitywrapperview(context context, attributeset attrs) {     super(context, attrs);      //check attributes need, example 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 values need indexes array attributes defined above     int leftpadding = arr.getdimensionpixeloffset(0, -1);     int toppadding = arr.getdimensionpixeloffset(1, -1);      //you can check if attribute exists (in examle checking paddingright)     int paddingright = arr.hasvalue(3) ? arr.getdimensionpixeloffset(3, -1) : mydefaultpaddingright;   } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -