Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to make a modal custom view?

I would like to make my own dialog, completely drawn myself, as a custom view. Can I display it in such a way that it displays above all other views and that all touch events, even those outside the dialog's area, are redirected to the dialog?

like image 980
futlib Avatar asked Feb 20 '11 15:02

futlib


2 Answers

I find that using a transparent activity and startActivityForResult() gives me a total freedom how I want my "dialog" to look and behave. So I suggest you check it out: How do I create a transparent Activity on Android?

With full screen and a darker background of your choice:

<style name="Theme.Transparent" parent="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">     
<item name="android:windowBackground">@color/transparentActivityBackground</item>    
</style>

Then in strings.xml:

<color name="transparentActivityBackground">#55000000</color>

If you want to blur the screen of the previous activity then use this line before setContentView:

this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); 
like image 63
Lumis Avatar answered Nov 12 '22 15:11

Lumis


This should be possible by setting the height/width of the root element to anything but fill_parent.

You could also achieve this by creating a custom theme that inherits from the built in android dialog theme.

http://developer.android.com/guide/topics/ui/themes.html

like image 40
AverageMarcus Avatar answered Nov 12 '22 17:11

AverageMarcus