How to Change the Background Color Around a Dialogfragment

How to change the background color around a DialogFragment?

I had to set android:windowIsFloating to false and android:windowBackground to my custom color in the dialog style:

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="MyDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@color/orange_transparent</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:gravity">center</item>
</style>

</resources>

MyDialogFragment

public class MyDialogFragment extends DialogFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
}
}

How do I make a Dialog Fragment's background invisible?

You can design the layout like following. There is an extra layout, but in case of dialogs, it will help

Sample Image

Changing dialogfragment background color (Using the Android Holo Colors Generator Xml Template)

I went crazy finding a solution for this problem until I came across a solution.

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Setup the layout
LayoutInflater inflater = getActivity().getLayoutInflater();
final View root = inflater.inflate(*"YOUR LAYOUT"*, null);
root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

//Customizing the dialog features
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(root);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(*"YOUR SELECTED COLOR"*)));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return dialog;
}

How to change DialogFragment header background color and bottom line color

You can actually find the divider after the dialog has been created:

AlertDialog dialog = builder.show();

// Set title divider color
int titleDividerId = getResources().getIdentifier("titleDivider", "id", "android");
View titleDivider = dialog.findViewById(titleDividerId);
if (titleDivider != null)
titleDivider.setBackgroundColor(getResources().getColor(android.R.color.holo_purple));

Customising the background of the header is slightly more complex... You need to define in your theme an alertDialogStyle defining how you draw each area of the dialog. For example:

<style name="Theme.Yours" parent="@android:style/Theme.Holo">
...
<item name="android:alertDialogStyle">@style/AlertDialog_Yours</item>
</style>

<style name="AlertDialog_Yours">
<item name="android:fullDark">...</item>
<item name="android:topDark">...</item>
<item name="android:centerDark">...</item>
<item name="android:bottomDark">...</item>
<item name="android:fullBright">...</item>
<item name="android:topBright">...</item>
<item name="android:centerBright">...</item>
<item name="android:bottomBright">...</item>
<item name="android:bottomMedium">...</item>
<item name="android:centerMedium">...</item>
</style>

Those areas can be colors or drawables, and to understand what they are, I redirect you to a blog post I wrote on the subject (part 5), explaining how you can theme everything.

Change background color of a Fragment from DialogFragment ColorPicker using Mutablelivedata not working

I found a solution. In Kotlin, data is not shared if viewModels() is instantiated without activityViewModels().
So I changed my code, instead of:

private val viewModel: MainViewModel by lazy {
getViewModel {
LightmvpViewModel()
}
}

I wrote:

private val viewModel: MainViewModel by activityViewModels()

With that simple change, everything should work.

Change background color of header in DialogFragment android

for custom layout:

https://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

AlertDialog dialog = builder.show();

// Set title divider color
int titleDividerId = getResources().getIdentifier("titleDivider", "id", "android");
View titleDivider = dialog.findViewById(titleDividerId);
if (titleDivider != null)
titleDivider.setBackgroundColor(getResources().getColor(android.R.color.holo_purple));

Customising the background of the header is slightly more complex... You need to define in your theme an alertDialogStyle defining how you draw each area of the dialog. For example:

<style name="Theme.Yours" parent="@android:style/Theme.Holo">
...
<item name="android:alertDialogStyle">@style/AlertDialog_Yours</item>
</style>

<style name="AlertDialog_Yours">
<item name="android:fullDark">...</item>
<item name="android:topDark">...</item>
<item name="android:centerDark">...</item>
<item name="android:bottomDark">...</item>
<item name="android:fullBright">...</item>
<item name="android:topBright">...</item>
<item name="android:centerBright">...</item>
<item name="android:bottomBright">...</item>
<item name="android:bottomMedium">...</item>
<item name="android:centerMedium">...</item>
</style>


Related Topics



Leave a reply



Submit