I\'m building a custom DialogFragment
. The dialog layout is set to my_dialog.xml
, but how can I modify the color around the dialog (the transparent
Those who are using AlertDialog builder in 'onCreateDialog
' instead of 'onCreateView
' can assign theme like following code. Complete set of themes can be found from R.style. Don't forget that some of them supported recently and are not available on old device phones.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Translucent);
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_album, null);
builder.setView(view);
return builder.create();
}
I found I just needed to do this:
<style name="MyDialog" parent="@android:style/Theme.Dialog">
<!-- other attributes -->
<item name="android:backgroundDimEnabled">false</item>
</style>
I wanted a transparent background for my DialogFragment
, and, in code, this works fine:
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
}
Of course, you can specify any color or Drawable
using setBackgroundDrawable()
or setBackgroundDrawableResource()
.
This works at least in onStart()
, but not in onCreate()
, and not necessarily in onCreateView()
, it seems.
That said, in most cases it's probably cleaner to do this in XML, using styles, along these lines:
<style name="MyDialogStyle" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
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);
}
}
Overriding onCreate and setting the style there should work.
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent);
}
Jul's answer was almost good for me. But it seems it doesn't work when using an AlertDialog.Builder.
I had to set the style here:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialog );