umesh
umesh

Reputation: 1168

Changing Background dim color for dialog appears in android

Hi I have to chage the dim color of dialog in my theme to white, so that by setting layoutParams.dimAmount = 0.5f; i can get blur white in dialog background.
I am using

<style name="MyDialog" parent="@android:style/Theme.Holo.Light.Dialog">
            <item name="android:windowBackground">@color/dialog_dim_background</item>
    </style>

and following below references
How do I create a transparent Activity on Android?
Custom screen dim with Dialog

Upvotes: 4

Views: 10880

Answers (3)

Namrata Bagerwal
Namrata Bagerwal

Reputation: 1210

I know it's very old question but this will help out surely to someone who is using AppCompatActivity or ActionBarActivity. If you are setting a dialog to your activity or say DialogActivity then also the below will work!

    dialog = new Dialog(ActivityName.this);
    dialog .setCancelable(false);
    dialog .setContentView(R.layout.dialog_layout);
    Window window = dialog.getWindow();
    if(window != null){
       window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
       window.setDimAmount(0.5f);
       //If you are setting a dialog activity
            DisplayMetrics displayMetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            Double dialogWidth = displayMetrics.widthPixels * 0.50;
            window.setLayout(dialogWidth.intValue(), 
                 WindowManager.LayoutParams.WRAP_CONTENT); //Setting it cover half of the screen width
   }
   dialog.show();

Just before you dismiss the dialog,

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.dismiss();

//If you are setting a dialog activity, create a style in styles.xml:

<style name="MyTheme" parent="@style/Theme.AppCompat.Light.Dialog">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">false</item>
</style>

And in your Manifest, set the theme:

<activity android:name=".view.POSSelectCustomerDialogActivity"
  android:theme="@style/MyTheme"
  android:windowSoftInputMode="stateAlwaysHidden|adjustResize"/>

Upvotes: 0

RexSplode
RexSplode

Reputation: 1505

It is a very old question, but i'd like to add n answer to it. I faced the same problem and googled a lot. Then came up with my own solution.

First of all, I removed the dim at all with this

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

And then I opened my layout(it was RelativeLayout) and put this view in it as the last element

<View
        android:id="@+id/shade"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/primaryShadow"
        android:visibility="invisible"/>

The background color is the color you want to use as your dim color. As you can see, it is invisible when created. So you'll need to find it in you Activity and set it's visibility before you launch the dialog to View.VISIBLE. When you cancel dialog, set it to View.INVISIBLE again. And that's it.

Upvotes: 5

Hala.M
Hala.M

Reputation: 766

Ok here is how I do that "I don't know how will these behave with the blur flag though"

  1. I create a custom layout for the dialog with a background color of my choosing .
  2. set the dialog to fill screen
  3. remove the dim behind flag

Code snippet

Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dailog_layout);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(0));
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
dialog.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND);

Best of luck

Upvotes: 1

Related Questions