Calling android dialog without it fading the background
Solution 1
Create a res/values/styles.xml file and add this to it.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.DoNotDim" parent="android:Theme">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
And apply the theme to your activity.
<activity android:name=".SampleActivity" android:theme="@style/Theme.DoNotDim">
Solution 2
You can also remove the dim effect by code with the following:
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Solution 3
Create a custom style and place it in your values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourCustomStyle" parent="android:Theme">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
To set a style to an individual dialog you can use
Dialog dialog = new Dialog(context, R.style.YourCustomStyle);
Solution 4
For displaying dialog like TrueCaller do this:
In styles.xml
file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myBackgroundStyle" parent="android:Theme.Dialog">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
In AndroidManifest.xml
do this:
<activity android:name=".SampleActivity" android:theme="@style/myBackgroundStyle">
Codejoy
Programmer and photographer, jack of all trades master of absolutely zilch!
Updated on January 29, 2020Comments
-
Codejoy over 4 years
I have this nice dialog view I set my UserInputDialog class to:
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/nameMessage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="What is your name Captain?" > </TextView> <EditText android:id="@+id/nameEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="18sp" > </EditText> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <Button android:id="@+id/okButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OK"> </Button> <Button android:id="@+id/cancelButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel"> </Button> </LinearLayout> </LinearLayout>
I want my dialog to show up but for the background to not fade out. Is this possible? Cause the view that calls this dialog has a neato background I would like to be shown as a backdrop to the dialog.
I found this online:
<style name="doNotDim" parent="@android:style/Theme.Dialog"> <item name="android:backgroundDimAmount">0</item> </style >
but not sure how to apply that to my dialog? I have a class called
public class UserInputDialog extends Dialog implements OnClickListener
. It sets its content view to the layout described above.I think I am doing this all right, just not sure how to add that style so I can NOT fade the background.
Secondary question: Can you get new looks on your dialog (by say having an image or icon display with your text there) by using Themes?
-
Codejoy about 14 yearswill that make every dialog that I call from that activity use the same style?
-
Robby Pond about 14 yearsActually that is a style setting on the Activity, meaning the Activity will not dim ever. Has nothing to do with the dialog.
-
Srujan Simha over 7 yearsThis's more clean and concise, should be the accepted answer.
-
Ashutosh Chamoli over 6 yearsReally helpful. Thanks :)
-
Ben.Slama.Jihed over 6 yearsWork perfectly and supported since api 3
-
Meow Cat 2012 over 5 yearsGreat! And
dialog.getWindow().setDimAmount(0.1f)
would have slight dim. Value should be within [0.0f, 1.0f]. -
Otziii almost 4 yearsRemember to do this after the dialog is available. I tried in onAttach and it did not work. In onViewCreated it worked :)