Saturday, March 5, 2016

android - How to create a Transparent Activity

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="50sp"
        android:textStyle="bold"
        android:fontFamily="sans-serif-condensed"
        android:text="This is a\nTransparent Activity"
        android:textColor="#f219b5"
        />
</RelativeLayout>
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cfsuman.me.androidcodesnippets"
    >

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/Theme.AppCompat.Transparent.NoActionBar"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
res/values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!-- Customize theme for transparent activity -->
    <style name="Theme.AppCompat.Transparent.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Transparent the theme here -->

        <!--
            android:windowIsTranslucent
                Flag indicating whether this is a translucent window.
        -->
        <!--
            public static final int Window_windowIsTranslucent
                Flag indicating whether this is a translucent window. If this attribute is unset
                (but not if set to false), the window might still be considered translucent,
                if windowSwipeToDismiss is set to true.

                Must be a boolean value, either "true" or "false".

                This may also be a reference to a resource (in the form "@[package:]type:name") or
                theme attribute (in the form "?[package:][type:]name") containing a value of this type.

                This corresponds to the global attribute resource symbol windowIsTranslucent.

                Constant Value: 5 (0x00000005)
        -->
        <item name="android:windowIsTranslucent">true</item>

        <!--
            android:windowBackground
                Drawable to use as the overall window background.
        -->
        <!--
            public static final int Window_windowBackground
                Drawable to use as the overall window background. As of HONEYCOMB, this may be a
                selector that uses state_accelerated to pick a non-solid color when running
                on devices that can draw such a bitmap with complex compositing on top at 60fps.

                There are a few special considerations to use when setting this drawable:

                This information will be used to infer the pixel format for your window surface.
                If the drawable has any non-opaque pixels, your window will be translucent (32 bpp).
                If you want to draw the entire background yourself, you should set this drawable to
                some solid color that closely matches that background (so the system preview of
                your window will match), and then in code manually set your window's background
                to null so it will not be drawn.

                Must be a reference to another resource, in the form "@[+][package:]type:name" or
                to a theme attribute in the form "?[package:][type:]name".

                This corresponds to the global attribute resource symbol windowBackground.

                Constant Value: 1 (0x00000001)
                
        -->
        <item name="android:windowBackground">@android:color/transparent</item>

        <!--
            android:windowContentOverlay
                This Drawable is overlaid over the foreground of the Window content area, usually
                to place a shadow below the title.
        -->
        <item name="android:windowContentOverlay">@null</item>

        <!--
            android:windowNoTitle
                Flag indicating whether there should be no title on this window.
        -->
        <item name="android:windowNoTitle">true</item>

        <!--
            android:windowIsFloating
                Flag indicating whether this is a floating window.
        -->
        <item name="android:windowIsFloating">true</item>

        <!--
            android:backgroundDimEnabled
                Control whether dimming behind the window is enabled.
        -->
        <item name="android:backgroundDimEnabled">false</item>
    </style>

</resources>