Question

Say i have an xml element like this:

<FrameLayout
    android:id="@+id/login_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.2">

i want that alpha value 0.2 to be a variable. What is the "Android way" of making the value a variable? do i stick it in dimen.xml? or does it even matter?

Would a proper way to do this be that i just create a file called transparency.xml and then link it with android:alpha="@transparency/my_value" ?

No correct solution

OTHER TIPS

You could do it as:

<resources>
    <item name="alpha_value" format="float" type="dimen">0.2</item>
</resources>

This way alpha_value will be in @dimen/ folder.

For more info, see the docs.

It works for me

in dimens.xml:

<resources>
  <item name="alpha_value" type="integer" format="float">0.8</item>
</resources>

In a layout:

  ...
  android:alpha="@integer/alpha_value"
  ...

In a code:

  R.integer.alpha_value

Since android:alpha takes integer types only, we will have to define it in dimens.xml as an item with integer type and float format, as follows:

<item name="alpha_login" type="integer" format="float">0.2</item>

Now it can be used just like an integer resource.

Use it in your layout file as:

android:alpha="@integer/alpha_login"

Use it in your code as:

TypedValue typedValue = new TypedValue();
resources.getValue(R.integer.alpha_login, typedValue, true);
float alphaLogin = typedValue.getFloat();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top