I updated my phone to Android 6.0 and I have these 2 problems with dialogs:
1)The title is shown but the messages isn't for alert dialog(SOLVED):
new AlertDialog.Builder(context).setTitle("Title").setMessage("Message");
2)Also custom dialog fragment's title is not shown(NOT SOLVED):
getDialog().setTitle("Title");
There was not such a problem in lollipop or in older versions, the problem appeared only after updating my phone to marshmallow.
How to solve the problem?
Use constructor with theme for Lollipop and newer android versions:
Dark theme
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
And for Light theme
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
I've come across a few answers that say that you should use the following in your app theme:
<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
While that is correct, it wasn't working in all places for me. Eventually, I realized that in some places I was using the regular AlertDialog builder and in other places I was using the support builder. If you are using the support AlertDialog, make sure to also include the following in your theme:
<item name="alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
Just put this in your styles file inside your Base Theme style tag and you are good to go
<item name="android:alertDialogTheme">@style/Theme.AppCompat.Light.Dialog.Alert</item>
Answer for 2)
, we need to call setStyle()
before onCreateDialog()
because theme
is used in onCreateDialog()
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setStyle(STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog_Alert);
}
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle(R.string.dialog_title);
return dialog;
}
i tried to fix this problem by making a style of my own.
your dialog object should be like this and your style pointed to this Dialog will be written below
new AlertDialog.Builder(new ContextThemeWrapper(Context, R.style.Dialog))
.setTitle("Title")
.setMessage("Sample Message ...").show();
R.style.Dialog ::
<style name="Dialog">
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColor">@color/primary_color</item>
</style>
Colors ::
<color name="primary_text">#212121</color>
<color name="primary_color">#2196f3</color>
finally the output should be something like this
Note : "android:textColorPrimary" goes for dialog setMessage and "android:textColor" goes for dialog setTitle ; i do not use setPositive and setNegative button and listener in my dialog object but you can see them in the picture if you want you can make your own dialog object.
new AlertDialog.Builder(context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
Hope this will help you.............
I suspect that you are ending up showing white text on a white background! (Looking at the screenshot, the (i) icon is not showing up well either, suggesting that it was designed to be shown on a background other than white.
You can use the constructor public AlertDialog.Builder (Context context, int themeResId)
to ensure you are using a specific theme to style your dialog, where the Theme_Material_Dialog
is probably what you want.
Maybe your main text color is the same as dialog background color, in that case in your styles.xml use (just an example):
<style name="AlertDialog" parent="@android:style/Theme.Material.Light.Dialog.Alert">
<item name="android:textColor">#000000</item>
</style>
and in dialog creation:
new AlertDialog.Builder(
new ContextThemeWrapper(getContext(), R.style.AlertDialog))
).setMessage("test");
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
Once check this hope this will work.......
In my case all dialogs had the same color as fonts (title & message). What I have done to fix this state was the change of the color attribute in my theme. I copied the xml theme file into the 'res/values-v22' and set different color for marshmallow.
<resources>
<style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:textColorPrimary">@android:color/tertiary_text_dark</item>
</style>
</resources>
or only for dialogs
<resources>
<style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:alertDialogTheme">@style/LywAlertDialogStyle</item>
</style>
<style name="LywAlertDialogStyle" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">@color/lyw_secondary_text_color</item>
</style>
</resources>
When using DialogFragment
you need to
Add style
<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog"> <item name="android:windowNoTitle">false</item> </style>
In your
DialogFragment
addgetTheme
public class CustomDialogFragment extends DialogFragment{ public int getTheme() { return R.style.CustomDialogFragment; } . . .
And that's it!
来源:https://stackoverflow.com/questions/33228454/android-6-0-dialog-text-doesnt-appear