Muhammed Refaat Muhammed Refaat - 4 months ago 26
Android Question

Using ScaleType enum values by its integer reference

To make my question way clear, I want to use

ScaleType
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
ScaleType
values are
enum
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:

android:scaleType="FIT_START"

Answer

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.

w720dp\sizes.xml

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

values\sizes.xml

<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:

 <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/advert_image"
        android:layout_width="match_parent"
        android:layout_height="230dp"
        android:layout_centerVertical="true"
        android:scaleType="@integer/advert_scaletype"
        android:transitionName="photo"/>

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:

 <com.android.volley.toolbox.NetworkImageView 
      ...
      android:scaleType="@dimens/advert_scaletype" />
Comments