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):
@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.......
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
add getTheme
public class CustomDialogFragment extends DialogFragment{
public int getTheme() {
return R.style.CustomDialogFragment;
}
.
.
.
And that's it!
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>
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.
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;
}