I've got these two items in my dimension definitions:
<dimen name="toolbar_search_extended_height">158dp</dimen>
<dimen name="toolbar_search_normal_height">?attr/actionBarSize</dimen>
Now I'd like to get the actual value in pixels at runtime:
height = getResources().getDimensionPixelOffset(R.dimen.toolbar_search_extended_height);
height = getResources().getDimensionPixelOffset(R.dimen.toolbar_search_normal_height);
The first call gives whatever 158dp is in pixels on the device.
The second call yields a NotFoundException
:
android.content.res.Resources$NotFoundException: Resource ID #0x7f080032 type #0x2 is not valid
Type 0x2
is: TypedValue#TYPE_ATTRIBUTE
:
/** The <var>data</var> field holds an attribute resource
* identifier (referencing an attribute in the current theme
* style, not a resource entry). */
public static final int TYPE_ATTRIBUTE = 0x02;
What is the preferred method to dereference dimen
values that can be either actual values or references to styled attributes?
Dimensions: are qualitative data. These are objects of the subjects. Dimension attributes : These are columns of dimension table. Facts : are the quantitative data.
The key attribute is the attribute in a dimension that identifies the columns in the dimension main table that are used in foreign key relationships to the fact table. Typically, the key attribute represents the primary key column or columns in the dimension table.
To add attributesDouble-click the Product dimension in Solution Explorer. In the Attributes pane, notice the Product Key attribute that was created by the Cube Wizard. On the toolbar of the Dimension Structure tab, make sure the Zoom icon to view the tables in the Data Source View pane is set at 100 percent.
By default, an attribute hierarchy is FullyOptimized, which means that SQL Server Analysis Services builds indexes for the attribute hierarchy to improve query performance. The other option, NotOptimized, means that no indexes are built for the attribute hierarchy.
This is what I implemented but it feels cumbersome and hacky:
private int getDimension(@DimenRes int resId) {
final TypedValue value = new TypedValue();
getResources().getValue(resId, value, true);
if (value.type == TypedValue.TYPE_ATTRIBUTE) {
final TypedArray attributes = getTheme().obtainStyledAttributes(new int[]{value.data});
int dimension = attributes.getDimensionPixelOffset(0, 0);
attributes.recycle();
return dimension;
} else {
return getResources().getDimensionPixelOffset(resId);
}
}
I was hoping that the framework would dereference my ?attr/
dimension directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With