Is there really no XML attribute counterpart to setAlpha(int)
?
If not, what alternatives are there?
Is there really no XML attribute counterpart to setAlpha(int)
?
If not, what alternatives are there?
No, there is not, see how the "Related XML Attributes" section is missing in the ImageView.setAlpha(int) documentation. The alternative is to use View.setAlpha(float) whose XML counterpart is android:alpha
. It takes a range of 0.0 to 1.0 instead of 0 to 255. Use it e.g. like
<ImageView android:alpha="0.4">
However, the latter in available only since API level 11.
It's easier than the other response.
There is an xml value alpha
that takes double values.
android:alpha="0.0"
thats invisible
android:alpha="0.5"
see-through
android:alpha="1.0"
full visible
That's how it works.
setAlpha(float)
and android:alpha
only since API 11 (Android 3.0). Prior API 11 one must use code to set alpha for image. As sschuberth already said in anser above. –
Hydroxy No, there is not, see how the "Related XML Attributes" section is missing in the ImageView.setAlpha(int) documentation. The alternative is to use View.setAlpha(float) whose XML counterpart is android:alpha
. It takes a range of 0.0 to 1.0 instead of 0 to 255. Use it e.g. like
<ImageView android:alpha="0.4">
However, the latter in available only since API level 11.
ImageView.setAlpha(int)
is taking an int
while android:alpha
is taking a float, so strictly speaking the latter is not the exact XML counterpart to the former, but it's the counterpart to View.setAlpha(float)
. And as mentioned multiple times here already, android:alpha
/ View.setAlpha(float)
are available as of API level 11 only. –
Currant I am not sure about the XML but you can do it by code in the following way.
ImageView myImageView = new ImageView(this);
myImageView.setAlpha(xxx);
In pre-API 11:
In API 11+:
alpha
does not have an XML-attribute counterpart when various sizes, positions do. –
Premiere Maybe a helpful alternative for a plain-colored background:
Put a LinearLayout over the ImageView and use the LinearLayout as a opacity filter. In the following a small example with a black background:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000" >
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_stop_big" />
<LinearLayout
android:id="@+id/opacityFilter"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CC000000"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
Vary the android:background attribute of the LinearLayout between #00000000 (fully transparent) and #FF000000 (fully opaque).
There is now an XML alternative:
<ImageView
android:id="@+id/example"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/example"
android:alpha="0.7" />
It is: android:alpha="0.7"
With a value from 0 (transparent) to 1 (opaque).
setAlpha(int)
is deprecated as of API 16
: Android 4.1
Please use setImageAlpha(int)
instead
use android:alpha=0.5 to achieve the opacity of 50% and to turn Android Material icons from Black to Grey.
Use this form to ancient version of android.
ImageView myImageView;
myImageView = (ImageView) findViewById(R.id.img);
AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
alpha.setDuration(0);
alpha.setFillAfter(true);
myImageView.startAnimation(alpha);
To reduce the opacity of anything in XML Android, use the Attribute Alpha. Example:
android:alpha="0.6"
You must enter the value between range of 0.0 to 1.0, in points.
The alpha can be set along with the color using the following hex format #ARGB or #AARRGGBB. See http://developer.android.com/guide/topics/resources/color-list-resource.html
© 2022 - 2024 — McMap. All rights reserved.
ImageView.setAlpha(int)
is taking anint
whileandroid:alpha
is taking a float, so strictly speaking the latter is not the exact XML counterpart to the former, but it's the counterpart toView.setAlpha(float)
. And as mentioned multiple times here already,android:alpha
/View.setAlpha(float)
are available as of API level 11 only. – Currant