102

I already know how to set the opacity of the background image but I need to set the opacity of my shape object.

In my Android app, I have it like this: enter image description here

and I want to make this black area a bit transparent, like here, for example I can see circles though this "Welcome..." :

enter image description here

Here is my shape code:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/shape_my"">
    <stroke android:width="4dp" android:color="#636161" />
    <padding android:left="20dp"
        android:top="20dp"
        android:right="20dp"
        android:bottom="20dp" />
    <corners android:radius="24dp" />
</shape>

How can I do that?

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
lomza
  • 9,412
  • 15
  • 70
  • 85

6 Answers6

228

In general you just have to define a slightly transparent color when creating the shape.

You can achieve that by setting the colors alpha channel.

#FF000000 will get you a solid black whereas #00000000 will get you a 100% transparent black (well it isn't black anymore obviously).

The color scheme is like this #AARRGGBB there A stands for alpha channel, R stands for red, G for green and B for blue.

The same thing applies if you set the color in Java. There it will only look like 0xFF000000.

UPDATE

In your case you'd have to add a solid node. Like below.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/shape_my">
    <stroke android:width="4dp" android:color="#636161" />
    <padding android:left="20dp"
        android:top="20dp"
        android:right="20dp"
        android:bottom="20dp" />
    <corners android:radius="24dp" />
    <solid android:color="#88000000" />
</shape>

The color here is a half transparent black.

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
  • http://codeviewer.org/view/code:1974 - shape.xml . http://codeviewer.org/view/code:1975 - layout.xml .http://codeviewer.org/view/code:1976 - .java The setOpacity does not work with shape. And I tryid to add android:background="#00000000" to shape.xml, but nothing happens. – lomza Apr 13 '11 at 09:44
  • This is odd... It doesn't matter what value I set to color, I have my shape fully transparent =( – lomza Apr 13 '11 at 10:02
  • 3
    It seems to me, that opacity can only be set to a view or an image... Because when I write android:background="#88000000" instead of android:background="@drawable/shape" in my LinearLayout, it works. – lomza Apr 13 '11 at 10:12
  • 2
    It works on shapes too. Have you tried what I've posted above? – Octavian Helm Apr 13 '11 at 10:22
  • 1
    Yes, but it does not work. Did you try all this code on your machine? – lomza Apr 13 '11 at 10:32
  • No I'm sorry I'm not going to do that as I have no time that plus this is not the scope of this site. I can assure you that the `solid` node works in combination with proper alpha settings. Don't try to set the alpha the way you do right now via Java. – Octavian Helm Apr 13 '11 at 10:35
  • Oh, gosh! You know what was the mistake??? android:color="#88000000", not just color="#88000000"! xD Oh, THANK YOU one more time! – lomza Apr 13 '11 at 10:43
  • You might also want to set `yourShape.mutate()` so that it doesn't share its state with any other drawable. Otherwise any changes to other objects using the same shape can affect the state/color of all the other shapes. .mutate() will treat them as individual shape objects with their own state. http://developer.android.com/reference/android/graphics/drawable/ShapeDrawable.html#mutate() – Max Worg Feb 28 '15 at 19:19
  • Sir I will frame this answer :) – Slobodan Margetić Oct 11 '20 at 12:24
16

Instead of convert alpha to a hex value, we can also use color state list to define alpha.

res/values/colors.xml

<color name="colorPrimary">#0000FF</color>

res/color/color_primary_20.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="0.2" android:color="?attr/colorPrimary" />
</selector>

res/drawable/a_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="2dp"
        android:color="@color/color_primary_20" />
</shape>
Linh
  • 57,942
  • 23
  • 262
  • 279
4

use this code below as progress.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#00000000" />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#00000000" />
            </shape>
        </clip>
    </item>

</layer-list>

where:

  • "progress" is current progress before the thumb and "secondaryProgress" is the progress after thumb.
  • color="#00000000" is a perfect transparency
  • NOTE: the file above is from default android res and is for 2.3.7, it is available on android sources at: frameworks/base/core/res/res/drawable/progress_horizontal.xml. For newer versions you must find the default drawable file for the seekbar corresponding to your android version.

after that use it in the layout containing the xml:

<SeekBar
    android:id="@+id/myseekbar"
    ...
    android:progressDrawable="@drawable/progress"
    />

you can also customize the thumb by using a custom icon seek_thumb.png:

android:thumb="@drawable/seek_thumb"
ungalcrys
  • 5,242
  • 2
  • 39
  • 23
2

Use this one, I've written this to my app,

<?xml version="1.0" encoding="utf-8"?>
<!--  res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <solid android:color="#882C383E"/>
    <corners
        android:bottomRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"/>
</shape>
1

To set alpha with a theme attribute color like as ?attr/colorSurface:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:tint="?attr/colorSurface"> <!-- specify your tint color -->
    <solid android:color="#AA000000" /> <!-- instead of `AA` specify your alpha level -->
</shape>

Full alpha changeel

enter image description here

Daniil Pavlenko
  • 153
  • 1
  • 9
0

If someone is trying with Cardview they can try this code.

<androidx.cardview.widget.CardView
            android:id="@+id/cvView"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginBottom="16dp"
            android:shape="ring"
            android:visibility="visible"
            app:cardCornerRadius="75dp"
            app:cardBackgroundColor="@android:color/transparent"
            app:layout_constraintBottom_toTopOf="@id/fab"
            app:layout_constraintEnd_toEndOf="@+id/fab"
            app:layout_constraintStart_toStartOf="@+id/fab">

            <ImageView
                android:id="@+id/ivImage"
                android:orientation="horizontal"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_gravity="center"
                android:padding="@dimen/_10sdp"
                android:background="@drawable/custom_circle_shape"
                android:src="@drawable/ic_fab_rpt"/>

</androidx.cardview.widget.CardView>

custom_circle_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#4D4953D2" />
    <corners android:radius="40dp" />
    <stroke
        android:width="@dimen/_2sdp"
        android:color="#7882FA" />
</shape>
minato
  • 2,028
  • 1
  • 18
  • 30