I have a class that looks like this:
public class MyDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("This is a title");
alertDialogBuilder.setMessage("This is a message");
alertDialogBuilder.setPositiveButton("Yes", this);
alertDialogBuilder.setNegativeButton("No", null);
return alertDialogBuilder.create();
}
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something
}
public void showAllowingStateLoss(FragmentManager fragmentManager) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(this, "MyDialogFragment");
ft.commitAllowingStateLoss();
}
}
And it is used like this:
MyDialogFragment dialog = new MyDialogFragment();
dialog.showAllowingStateLoss(fragmentManager);
The problem is that the message isn't shown in the app, it just displays this:
What am I doing wrong?
I found the solution myself. Apparently the dialog use android:textColorPrimary
from the theme as the text-color, and that color is white in my app. That results in an alertdialog that contains white text on a white background.
The solution was thus to add this in theme.xml:
<style name="MyAndroidTheme" parent="Theme.AppCompat.Light">
<item name="android:alertDialogTheme">@style/MyAlertDialogTheme</item>
</style>
<style name="MyAlertDialogTheme" parent="MyAndroidTheme">
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:windowBackground">@android:color/white</item>
<item name="android:windowIsFloating">true</item>
</style>
I don't really know why android:windowIsFloating
is needed, but without it, the dialog becomes fullscreen.