Android: How to make an activity background transparent/translucent

I'm currently in the process of updating my File Dialog selection library and found a way to give it a subtle facelift.

Much like how Dropbox has this beautiful floating Dialog that doesn't seem to belong to any specific Activity, we too can do this!

For your transparent activity, add "android:theme" to the AndroidManifest.xml declaration:

<activity android:label="@string/app_name" android:name="com.lamerman.FileDialog" android:theme="@android:style/Theme.Translucent.NoTitleBar">

The Translucent.NoTitleBar theme is already part of the SDK, so no need to create any new themes. As a reminder, if you're using activities from a library you'll have to set this on the AndroidManifest.xml declaration in YOUR app also.

This will work just fine up until API level 10 (Android 2.3 Gingerbread).  Once you hit Honeycomb, or when most people would realise Ice-cream Sandwich, this won't work.

Fortunately, there's not much prep-work required for the Activity.

public void onCreate(Bundle savedInstanceState) {

getWindow().setBackgroundDrawable(new ColorDrawable(0));

// ...


Screenshot_2013-08-22-09-34-46 Screenshot_2013-08-22-09-10-47

On the left we have the invisible translucent activity, even though it's used as a library. Right screenshot shows the app running from the launcher. I couldn't get it to shrink for some reason, but at the very bottom you can see phone, contacts, messaging and Chrome showing through.

This code will work back to Android API 4, which is pretty much standard with most things these days.


Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog