How to change the color of the default fragment dialog theme

I showed a fragment of the dialogue. I did this, but I want to change the background color (Theme) from gray to white.

enter image description here My dialogue code:

public class TestDialog extends DialogFragment {

public static TestDialog newInstance() {
    return new TestDialog();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return container;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            //setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.dailog_fragment, null))
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            TestDialog.this.getDialog().cancel();
        }
    });      
    return builder.create();
}

}

+4
source share
3 answers

I have a solution:

I use the style theme below to set the background color to white.

<style name="MyFragment">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:colorBackground">@android:color/white</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">true</item>
    </style>

We can also use the Holo theme.

AlertDialog.Builder builder = new AlertDialog.Builder(ProfileEditActionBar.this, AlertDialog.THEME_HOLO_LIGHT);
+2
source

use this

setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);

and then create your own style MyDialog.

<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>

note that he must be in style.xml.

+1
source

In your manifest file, determining the activity of the dialog activity, add the attribute android:themeas follows.

<activity
        android:name=".YourActivityName"

        android:theme="@styles/style"

        />
0
source

All Articles