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 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!

15 comments :

  1. I was helpful in making my project initial steps.
    Thanks

    ReplyDelete
  2. plzzz where to make this tell me

    ReplyDelete
    Replies
    1. i mean, the line under the screenshot, activity....where that activity to b performed

      Delete
  3. in what lines can find or put dis codes?

    ReplyDelete
  4. Good demo.
    Can you send me the code for displaying a translucent image at a particular position on screen on to the other image in the background??
    I want to see both the images, one in full screen in the background and other as translucent image at the particular position on the screen. hope u got my point.
    Thanks in advance.

    ReplyDelete
    Replies
    1. I don't have any code but you can set that layout parent as transparent using color code- #00000000.

      Delete
  5. Hello,
    good quick demo thanks.
    I'm pretty sure I've got everything in the right place but i get this error:
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homewin7.myapp3/com.example.homewin7.myapp3.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    All i have done is set the Manifest to have the android:theme="@android:style/Theme.Translucent" and the activity_main with the "#80000000".
    This was a new project .. blank activity. set to 4.1 compatibility. no other changes. If i remove the Translucent all works fine.

    ReplyDelete
    Replies
    1. I think you are using android studio v7 support library -

      compile 'com.android.support:appcompat-v7:23.0.1'

      right?

      so now replace this from your build.gradle with v4 support library.

      Delete
    2. Thanks for the quick response, sorry bit past my level of expertise yet, long time programmer but 3 days in to Android....
      In the MainActivity.java i have import [android.support.v7.app.AppCompatActivity;]
      Is this what needs to be changed.
      in the build.gradle there is
      dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:23.1.0'
      How do I find what the v4 name is please
      Sorry for the noob questions, any thanks in advance ;-)

      Delete
    3. Step 1- Open your build.gradle file and replace below line-
      compile 'com.android.support:appcompat-v7:23.1.0'
      with this line-
      compile 'com.android.support:support-v4:21.0.3'

      Step 2- From the res/values folder open the styles.xml file and change below line-
      style name="AppTheme" parent="Theme.AppCompat.Light">
      with-
      style name="AppTheme" parent="Theme.Black">

      Step 3- Open your Manifest.xml file and with your activity use below theme-
      android:theme="@android:style/Theme.Translucent"

      That's all!

      Delete
  6. Thanks Manish.
    it is working now.
    Cheers Willy

    ReplyDelete

Note: Only a member of this blog may post a comment.