Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dim the background of an activity in android

Tags:

android

I have a problem that I am showing an activity over another activity with transparent background but I want to show the same with dim background. How can I achieve this?

like image 469
Sanat Pandey Avatar asked Nov 29 '11 08:11

Sanat Pandey


People also ask

What is restrict background activity?

Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.

How can I dim the background when Bottomsheet is displayed without using dialog?

Using the Interface - onSlide which as a parameter slideOffSet of type float , can be used to dim the background.

Can Android activity run in background?

It seems like you want to run an activity in background when it quits. However, activity can't be run unless it's on foreground. In order to achieve what you want, in onPause(), you should start a service to continue the work in activity. onPause() will be called when you click the back button.


2 Answers

Here is the complete code:

    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.dimAmount = 0.75f;
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    getWindow().setAttributes(layoutParams);

Put this code right after you inflate the layout.

like image 136
John P. Avatar answered Sep 23 '22 00:09

John P.


Create a xml under drawable folder names as window_dim.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#000000" />
</shape>

Set main layout attribute in xml as bellow -

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
...

android:foreground="@drawable/window_dim"
android:orientation="vertical"
... >

In activity set main layout as bellow -

mainLayout.getForeground().setAlpha(180);
like image 29
Biswajit Karmakar Avatar answered Sep 20 '22 00:09

Biswajit Karmakar