Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the attr reference in code?

I'm looking to get the pointing reference from an attribute via code. In my xml layouts I can easily get the referenced drawable like this:

android:background="?attr/listItemBackground" 

The attribute reference is set by my Theme. I'm looking to see if it's possible to get that referenced drawable via code.

I can work around this by creating style attr and reading the value inside a custom view but in this case I want to figure out if this is possible without doing all that. I would think it would be possible but I haven't found ways to get that attribute reference.

Thanks!

like image 292
Jona Avatar asked Feb 22 '12 16:02

Jona


People also ask

How do you use ATTR?

The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element. When this method is used to set attribute values, it sets one or more attribute/value pairs for the set of matched elements.

What is ATTR XML?

The XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element.

What is the return type of attributeName?

.attr( attributeName )Returns: String. Description: Get the value of an attribute for the first element in the set of matched elements. The name of the attribute to get. The .attr() method gets the attribute value for only the first element in the matched set.

What is the difference between attR and prop in jQuery?

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method. The difference between attributes and properties can be important in specific situations.

How to get the value of an attribute for each element?

Description: Get the value of an attribute for the first element in the set of matched elements. The name of the attribute to get. The .attr() method gets the attribute value for only the first element in the matched set. To get the value for each element individually, use a looping construct such as jQuery's .each() or .map() method.

What is a checked attribute in HTML?

According to the W3C forms specification, the checked attribute is a boolean attribute, which means the corresponding property is true if the attribute is present at all—even if, for example, the attribute has no value or is set to empty string value or even "false". This is true of all boolean attributes.


1 Answers

This is how you do it:

// Create an array of the attributes we want to resolve // using values from a theme int[] attrs = new int[] { R.attr.listItemBackground /* index 0 */};  // Obtain the styled attributes. 'themedContext' is a context with a // theme, typically the current Activity (i.e. 'this') TypedArray ta = themedContext.obtainStyledAttributes(attrs);  // To get the value of the 'listItemBackground' attribute that was // set in the theme used in 'themedContext'. The parameter is the index // of the attribute in the 'attrs' array. The returned Drawable // is what you are after Drawable drawableFromTheme = ta.getDrawable(0 /* index */);  // Finally, free the resources used by TypedArray ta.recycle(); 
like image 109
Martin Nordholts Avatar answered Sep 28 '22 03:09

Martin Nordholts