How to Create a Transparent Activity on Android

How do I create a transparent Activity on Android?

Add the following style in your res/values/styles.xml file (if you don’t have one, create it.) Here’s a complete file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>

(The value @color/transparent is the color value #00000000 which I put in the res/values/color.xml file. You can also use @android:color/transparent in later Android versions.)

Then apply the style to your activity, for example:

<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>

How to create transparent activity in android?

There are two ways to achieve this

  • use the following theme for activity.

android:theme="@android:style/Theme.Translucent"

  • Set background of the activity as a trans parent png image or a transparent code

eg.

android:background="@drawable/transparent_bg"

or

android:background="#33BBFFFF"

this is a semi transparent color

Related Links

How to make a background 20% transparent on Android

Understanding colors on Android (six characters)

  • To dismiss activity on tap implement onTouchListener and when touch event is detected call finish();

Hope it helps !!

How to display transparent activity on the another activity without removing previous activity

declare your activity in manifest like this

 <activity android:name=".yourActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>

and add a transperent background to your layout
like this

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background = "any tranparent image name" >
</RelativeLayout>

Edit:

i think you are using this to open your transparent activity it finish your previous activity

Intent intent =new Intent(mContext,yourNewActivity.class);
startActivity(intent);
finish();

remove finish from here then your new activity in on top of previous activity like this

 Intent intent =new Intent(mContext,yourNewActivity.class);
startActivity(intent);

Hope help..

Activity with transparent background

After setting LinearLayout transparency try to set it's child transparency to the value that you want.
This should make them have different transparency than LinearLayout. Try it.

Is there a way to make an activity transparent in Android Studio?

You can achieve this through multiple ways

  1. Create an activity and make its background as transparent in the layout.(Not recommended)

  2. Create an alert dialog within the activity and make the alert dialog background as transparent

  3. Create a dialog fragment make its layout transparent and open it from the activity.

  4. Create a view stub within the same activity layout and inflate the view when required. (Handling back press events might be a difficult task here).

Although the right way would be to create an alert dialog within the activity or creating a dialog fragment or create a view stub. Create an alert dialog if you don't have much events or elements within the dialog since its easy and efficient than creating a dialog fragment for a little dialog. Creating a view stub would be the most efficient way since it simply inflates the view which takes less amount of resource. But don't go with creating an activity for this dialog which is resource intensive and not the correct way.

How do you create a transparent activity that can overlay the home screen but is not dismissed when the home or back button is pressed?

You can create a transparent activity with the help of

  1. Make the background of layout in your xml file transparent by using

    android:background="@android:color/transparent

  2. And also, make the theme in your manifest file transparent for that particular activity

    <activity
    android:name="Your activity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
    </activity>

  3. And for back press override the onBackPressed() method and remove super.onBackPressed()

    @Override
    public void onBackPressed()
    {
    // TODO Auto-generated method stub
    }

Android: how to create a transparent dialog-themed activity

You can create a tranparent dialog by this.

public class DialogActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle(" ");
alertDialog.setMessage("");
alertDialog.setIcon(R.drawable.icon);
alertDialog.setButton("Accept", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setButton2("Deny", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
}

After this just put a line in AndroidManifest.xml, where you define your activity in manifest.

android:theme="@android:style/Theme.Translucent.NoTitleBar"


Related Topics



Leave a reply



Submit