Muhammed Refaat Muhammed Refaat - 1 year ago 104
Android Question

Using ScaleType enum values by its integer reference

To make my question way clear, I want to use

in layout xml with different values according to layout width (e.g sw-600, sw-720, ...etc), and as it doesn't has a clear constant values like layout weights for example I don't know if it is possible to achieve that, so I had a look about ImageView class and found that the
values are
with assigned values, so is it possible to use the enum its assigned values? so when use it in the xml it will be

android:scaleType="2" // 2 will be an Integer value assigned differently across different layouts

instead of:


Answer Source

This is what I've done and so far so good. I have one resource that contains different values depending on the res folder type.


<integer name="advert_scaletype">3</integer>


<integer name="advert_scaletype">6</integer>

By doing this I get different scale type on the image if I'm on tablet or phone.

The way I use it:


I haven't found any issues so far so not sure why this is not working for other people.

In the case of the value you want to use is float, you can store it inside dimens.xml in the following format:

<item name="advert_scaletype" format="float" type="dimen">7.6</item>

and then it will be like:

      android:scaleType="@dimens/advert_scaletype" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download