Reputation: 2246
I am setting alpha to a bitmap
in XML
(selector) and its working fine on API level 23 , 22 , 21
but its doesn't work on API level 20
and below API levels
.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--State Selected-->
<item android:drawable="@drawable/item_selected" android:state_selected="true"/>
<!--State Normal-->
<item >
<bitmap android:src="@drawable/item_selected" android:alpha="0.6"/>
</item>
Edit
Please suggest me some alternatives to do this by using XML
.
Upvotes: 0
Views: 1469
Reputation: 89
bitmap element's alpha attribute was added at API 21 so there is no way setting alpha in the xml. you could set alpha using the code according to Janki Gadhiya's answer.
Upvotes: 1
Reputation: 4510
Setting alpha
programmatically will work. Try this.
I have tried your selector as the background of this ImageView
:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/select"
/>
My java code :
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setAlpha(0.5f);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, ""+imageView.isSelected()
, Toast.LENGTH_SHORT).show();
if(imageView.isSelected())
{
imageView.setSelected(false);
// set alpha 0.5
imageView.setAlpha(0.5f);
}
else {
imageView.setSelected(true);
// set alpha 1
imageView.setAlpha(1f);
}
}
});
Upvotes: 2