Monday, May 19, 2014

Android Transparent Background | Set Opacity in Android | Make android application with transparent background

Hi Friends,

I am again here with very small and important demo in android. How to make your activity transparent and how to set opacity in background.  I am sharing code here step by step-


1)      In your manifest make that activity theme to Translucent-
          <activity
            android:name="com.androidhub4you.transparent.background.MainActivity"
            android:theme="@android:style/Theme.Translucent"
            android:label="@string/app_name" >

2)      Now your page will be 100% transparent, so if you need some background color then set opacity like-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#80000000"
    tools:context=".MainActivity" >

Concentrate on this line- android:background="#80000000"

Here 80 stand for alpha color from 0-255 and 000000 for black color. So just add 2 first digit of alpha with your color code. Below is the normal chart for opacity percentage-

100% — FF
90% — E6
80% — CC
70% — B3
60% — 99
50% — 80
40% — 66
30% — 4D
20% — 33
10% — 1A
0% — 00

And below is the whole code-
   
1)    MainActivity.java

package com.androidhub4you.transparent.background;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
       }

      
}


    2)    Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#8C000000"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="www.androidhub4you.com\n(From: Manish Srivastava)"
        android:textColor="#FFFFFF"
        android:textSize="25sp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignLeft="@+id/textView1"
        android:src="@drawable/logo" />

</RelativeLayout>


     3)    AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidhub4you.transparent.background"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidhub4you.transparent.background.MainActivity"
            android:theme="@android:style/Theme.Translucent"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Your comments and feedback are awaited!

Thank You!