0

I'm a beginner on Android Studio and I need your help to apply a specific transparency on my layout.

I applied a background and border color to my layout by using this drawable:

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/black" />
    <corners android:radius="3dp" />
    <stroke android:width="5px" android:color="#ffa500" />
</shape>

Now I want to apply dynamically a transparency to this button (by using a seek bar) but only on the background color I don't want to modify my border color. I tried to use setAlpha function but it also change my border color transparency.

How can I do this? Thanks.

Avenger
  • 89
  • 1
  • 2
  • 9
  • Possible duplicate of [Set android shape color programmatically](https://stackoverflow.com/questions/17823451/set-android-shape-color-programmatically) – Bö macht Blau Jan 28 '19 at 17:47

2 Answers2

1

You need to use the alpha portion of the color your specify in item.

For example - #aarrggbb -- aa part is the alpha.

If you want to change stroke / solid color separately, I recommend using GradientDrawable to setStroke() and `setColor()'.

For reference -

https://developer.android.com/reference/android/graphics/drawable/GradientDrawable#setColor(int)

https://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setStroke(int,%20android.content.res.ColorStateList,%20float,%20float)

Vishal Arora
  • 2,524
  • 2
  • 11
  • 14
1

In your shape change the <solid android:color="@android:color/black" /> to <solid android:color="#00FFFFFF" /> This will make your shape transparent. And in your xml <Button .... android:background="@drawable/your_shape" />

Maksim Novikov
  • 835
  • 6
  • 18
  • Thanks for your reply. I forgot to specify that I want to dynamically change the transparency by using a seek bar. – Avenger Jan 28 '19 at 15:53
  • Ok but if i understand correctly you want to start from transparent? to dynamically change color is controlled from java class. open new question with corresponding problem description. – Maksim Novikov Jan 28 '19 at 15:56