Home > Mobile >  How to make custom rectangle shape with hex color of any color with opacity?
How to make custom rectangle shape with hex color of any color with opacity?

Time:11-30

Is their any possibility to draw rectangle shape with any of hex color with opacity for that color

for example: blue_color_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid
        android:color="#29618E"/>
    <corners
        android:radius="@dimen/_10sdp"/>

</shape>

The above code is normal rectangle shape with radius 10dp

But I want the give opacity for the shape without changing the hex code. And how to achieve this

CodePudding user response:

If you don't want to change a color, you can add android:alpha property to your View. For example:

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/blue_color_backgroung"
    android:alpha="0.5"/>

CodePudding user response:

The opacity attribute in android XML is alpha you can set alpha for a view in the range of 0.0 to 1.0 which 0.0 is fully transparent. just use like this :

android.alpha="0.7"

CodePudding user response:

Drawable customeShape = 
getResources().getDrawable(R.drawable.custom_shape);

 // setting the opacity (alpha)

customeShape.setAlpha(10);

// setting the images on the ImageViews

image.setImageDrawable(customeShape);
  • Related