zer0stimulus
zer0stimulus

Reputation: 23626

Android: How to programmatically make an Activity Window transluscent?

Is there a way to programmatically make an Activity Window appear transluscent? For my purposes, I cannot use static XML resources.

I attempted to set the background resource to a transluscent color, but that only make the background appear solid black.

Upvotes: 12

Views: 19663

Answers (5)

seadowg
seadowg

Reputation: 166

API 30 added a setTranslucent method for achieving this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    setTranslucent(true)
    window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}

Upvotes: 1

liubaoyua
liubaoyua

Reputation: 144

Finally, I find a way to solve this problem.

https://github.com/ikew0ng/SwipeBackLayout/blob/e4ddae6d2b8af9b606493cba36faef8beba94be2/library/src/main/java/me/imid/swipebacklayout/lib/Utils.java

if you want to make a activity translucent

Utils.convertActivityToTranslucent(activity);

or if you want to make it opaque

Utils.convertActivityFromTranslucent(activity);

Upvotes: 6

Nicola Desogus
Nicola Desogus

Reputation: 21

A bit late, but for posterity's sake...you can set the translucent theme programmatically like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    // Set a Translucent NoTitleBar theme before calling super.onCreate()
    setTheme(android.R.style.Theme_Translucent_NoTitleBar);

    super.onCreate(savedInstanceState);
}

Upvotes: 2

Gangadhar Nimballi
Gangadhar Nimballi

Reputation: 1534

write it in your activity class

Window window = this.getWindow();
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

Upvotes: 15

Related Questions