Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Custom shaped dialog

I am trying to create a custom shaped dialog in android. What I want is instead of it being rectangularly shaped, to have any shape I might want to create. Like put a custom background which is a png image in the shape of a circle.

If I do this, the area outside the circle gets filled with white to fill the rectangle of the dialog. What I need is to have only the circle and the rest of the layout to be hidden. Hope this makes sense.

From what I know this is not possible but still maybe someone has some good ideas ? Thanks.

like image 594
Fofole Avatar asked Feb 22 '23 10:02

Fofole


2 Answers

The way to go around it is to have a custom dialog with a transparent background colour (ARGB #00000000 or Color.Transparent). After that, add a linear layout to your custom dialog with an XML drawable for a background. In that XML, specify border radius to make the layout a circle. Next, add another layout to that linear layout with both width and height set to FILL_PARENT and background set to your circular image. Finally add the rest of your dialogue components to this second layout.

I remember achieving this effect in the past, but don't have the code handy to see the exact syntax.

like image 156
Aleks G Avatar answered Feb 24 '23 00:02

Aleks G


Well, I actually found what I want:

final Dialog d = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
        d.setContentView(R.layout.custom);
        d.setCanceledOnTouchOutside(true);
        d.setCancelable(true);
        return d;

The dialog constructor let me put it transparent and then I could do anything that I want in my layout with the background a png image with any shape I want. No android shapes or borders needed. This covers easily any shape not just circles/rect etc. as long as you manage your layout accordingly to your dialog design.

like image 32
Fofole Avatar answered Feb 23 '23 23:02

Fofole