Reputation: 6680
I have a shape defined in a drawable resource file that I use as a background for my TextViews (scrim_background.xml):
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/colorAccent"/>
</shape>
My colors.xml is:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#fff</color>
<color name="light_text">#dfff</color>
<color name="colorPrimary">@color/primary</color>
<color name="colorPrimaryDark">@color/primary_dark</color>
<color name="colorAccent">@color/accent</color>
<color name="primary">#F44336</color>
<color name="primary_dark">#D32F2F</color>
<color name="primary_light">#FFCDD2</color>
<color name="accent">#607D8B</color>
<color name="accentAlpha">#DD607D8B</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#757575</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#BDBDBD</color>
</resources>
The styles for my textviews is:
<style name="BeerItemTextStyleTitle" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">@color/light_text</item>
<item name="android:textStyle">bold</item>
<item name="android:background">@drawable/scrim_background</item>
<item name="android:gravity">start|center_vertical</item>
<item name="android:paddingStart">@dimen/space_small</item>
<item name="android:paddingEnd">@dimen/space_small</item>
</style>
I want that the background color to have 50% transparency of the colorAccent.
Something like:
<color name="accent">#88607D8B</color>
But I don't want to fix these values because I'm constantly changing the color scheme.
I tried to add alpha directly to my TextView, like:
<item name="android:alpha">50</item>
But it was not appropriate as it add the transparency to Text Color as well. I would like that only the background color were transparent.
I would not like to do this programmatically, because this way I would have to find all my textviews and change one by one.
-- EDIT --
I tried to create a meta resource file named bitmap_scrim_background that is:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="@drawable/scrim_background">
</bitmap>
</item>
</layer-list>
<style name="BeerItemTextStyleTitle" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">@color/light_text</item>
<item name="android:textStyle">bold</item>
<item name="android:background">@drawable/bitmap_scrim_background</item>
<item name="android:gravity">start|center_vertical</item>
<item name="android:paddingStart">@dimen/space_small</item>
<item name="android:paddingEnd">@dimen/space_small</item>
</style>
But i think things doesn't work this way in Android... It throws:
android.view.InflateException: Binary XML file line #36: Binary XML file line #36: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class TextView
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #5: <bitmap> requires a valid 'src' attribute
Upvotes: 5
Views: 2172
Reputation: 181
Using this to create scrollbar thumb 50% transparent with existing non transparent color
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:tint="@color/color_white_alpha_50" //#80FFFFFF
android:tintMode="multiply">
<solid android:color="?colorControlNormal" />
<corners android:radius="2dp" />
</shape>
Upvotes: 2
Reputation: 653
Have you looked at ColorUtils.setAlphaComponent()
?
int setAlphaComponent (int color, int alpha)
You could query any color with:
int x = getColor(R.color.yourColor)
Then create any alpha version of that color with:
int newX = ColorUtils.setAlphaComponent(x, newAlpha);
Upvotes: 1
Reputation: 1171
You can do that programmatically using:
textView.getBackground().setAlpha(50);
Upvotes: 0
Reputation: 723
I am guessing the scrim background is a drawable xml file. You can use the accentColor inside your drawable file.
For example
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:centerY="0.3"
android:startColor="@color/accent"
android:endColor="#00000000"
android:type="linear"/>
</shape>
Upvotes: 1