Mysterious_android
Mysterious_android

Reputation: 618

How to set Color of Dim Background for Dialog in Android Programmatically

I want to change the Dim Background to blue or red, every example i've looked at used an XML file, but is there a way to set by just passing in a Color String?

Thanks

Upvotes: 0

Views: 6767

Answers (3)

Shrikant
Shrikant

Reputation: 589

Try this: you can pass color code to ColorDrawable.

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Upvotes: 0

Adnan Maqbool
Adnan Maqbool

Reputation: 452

There is No direct way workaround but this may help you

Custom Dialog theme like this:

styles.xml

<style name="CustomDialogTheme" parent="android:Theme.Dialog">
 <item name="android:windowNoTitle">true</item>
 <item name="android:windowIsFloating">false</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

windowNoTitle use to get rid from title bar. window backgroud is set to transparent windowIsFloating is false to expand dialog view to full screen.

Custom Dialog.java

public CustomDialog(Context context) {
    super(context, R.style.CustomDialogTheme);
    setContentView(R.layout.custom_dialog);
}

custom_dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dim_dialog_bg_color">

    <RelativeLayout
        android:id="@+id/dialog_root"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/dialog_bg_color"
        android:padding="16dp">

</RelativeLayout>

Upvotes: 2

Safal Kumar Ghimire
Safal Kumar Ghimire

Reputation: 318

change the style of the dialog and use it

 <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <!-- Customize your theme here. -->
    <item name="android:windowBackground">@color/red</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:clipToPadding">false</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:textColorPrimary">@color/red</item>
 </style>

Upvotes: 1

Related Questions