Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a dimension value that is a reference to an attribute?

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?

like image 296
Benoit Duffez Avatar asked Feb 15 '15 07:02

Benoit Duffez


People also ask

Is dimension same as attribute?

Dimensions: are qualitative data. These are objects of the subjects. Dimension attributes : These are columns of dimension table. Facts : are the quantitative data.

What identifies attribute in a dimension?

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.

How do you add attributes to dimensions?

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.

What is the default option for the attribute hierarchy for the optimized state?

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.


1 Answers

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.

like image 179
Benoit Duffez Avatar answered Sep 28 '22 22:09

Benoit Duffez