Reputation: 23626
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
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
Reputation: 194
Use this method convertActivityFromTranslucent
https://github.com/ikew0ng/SwipeBackLayout/blob/e4ddae6d2b8af9b606493cba36faef8beba94be2/library/src/main/java/me/imid/swipebacklayout/lib/Utils.java
Upvotes: 0
Reputation: 144
Finally, I find a way to solve this problem.
if you want to make a activity translucent
Utils.convertActivityToTranslucent(activity);
or if you want to make it opaque
Utils.convertActivityFromTranslucent(activity);
Upvotes: 6
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
Reputation: 1534
write it in your activity class
Window window = this.getWindow();
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Upvotes: 15