Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a layer with black transparent over an activity

Tags:

android

I want to create a layer with transparent layer over an activity and I need to make visible only a particular view, say for an example "Login Button". It something mean that making hole on layer exactly the size of the button. Please anyone help me out of this.

Thanks In advance.

like image 718
Sathish Avatar asked Jun 13 '13 03:06

Sathish


2 Answers

If I understand your question correctly

Your parent Layout of the Activity will be a RelativeLayout. In your parent layout at the bottom add another child RelativeLayout that implments the match_parent parameters for both width and height. You can then set a color like this #99000000 to it's background. This will give you a black transparent layer over your parent.

Something like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <!-- here you add your activities views ect... -->

    <RelativeLayout
        android:id="@+id/childLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#99000000">
        <!-- This will be your overlay so here you can add your login button ect -->
    </RelativeLayout>

</RelativeLayout>

I didn't test this but it should solve your issue

like image 141
the-ginger-geek Avatar answered Jan 03 '23 13:01

the-ginger-geek


Programatically, you can use Canvas to create a layer. Fill the whole thing with your color and cut out a hole.

public class DrawView extends View {
    Paint paint = new Paint();
    Paint transparentPaint = new Paint;
    public DrawView(Context context) {
        super(context);            
    }

    @Override
    public void onDraw(Canvas canvas) {
        //first fill everything with your covering color
        paint.setColor(yourTransparentColor);
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
        //now clear out the area you want to see through
        transparentPaint.setAlpha(0xFF);
        transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        Rect rect=new Rect(left, top, right, bottom);//make this your rect!
        canvas.drawRect(rect,transparentPaint);
    }
}
like image 23
HalR Avatar answered Jan 03 '23 12:01

HalR