2

I'm creating a simple DialogFragment with a custom adapter but strangely the text appears white when the dialog shows up.

class MyDialog extends DialogFragment {

        ListView listView;

        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            ArrayAdapter<Service> adapter = new MyAdaper(getContext(), items);
            TextView title = new TextView(getContext());
            title.setText("Hello");
            title.setGravity(Gravity.CENTER);
            title.setTextSize(18);
            title.setTextColor(Color.BLACK);
            builder.setCustomTitle(title);
            builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            return builder.create();
        }

The layout xml inflated in the adapter is very simple and only has one TextView (I didn't specify any color but I would expect it to be black as the rest of my application when I use a TextView without specifying colors)

I have tried using

AlertDialog.Builder(getActivity(), R.style.MyTheme);

But even if I specify b black textColor in MyThem the Dialog still shows up with white text

What Am I missing ?

Max Young
  • 1,522
  • 1
  • 16
  • 42
Johny19
  • 5,364
  • 14
  • 61
  • 99

3 Answers3

1

Look you can create style for your dialog. For example:

  <style name="cust_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowTitleStyle">@style/dialog_title_style</item>
  </style>

  <style name="dialog_title_style" parent="android:Widget.TextView">
    <item name="android:background">@android:color/holo_red_dark</item>
  </style>

Than set up it into onCreate

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.cust_dialog);
  }
Silvestr
  • 704
  • 4
  • 13
  • Sorry if It wasn't clear enough in the question. The problem is not the title but the content of the dialog (the part inflated from the adapter). Even though, I had already tried defining a them and then setting it when creating the Builder. but the text still remain white! (Btw, setStyle doesn't seem to do anything, however new Builder(activity, them) does but cannot manage to change bloody text color to not white – Johny19 Dec 30 '15 at 08:56
1

<item name="android:textColorPrimary">@color/your desired color</item>

This line of code in your styles.xml file in Alert Dialog style will help in changing font color of message body in our AlertDialogBox.

0

Try builder.setMessage(R.id.<yours id-name of TextView>); But must have define the color of TextView in your XML code.

coffeeCode
  • 22
  • 4
  • That part of title works, it appears black. The white text is the text from the xml inflated from the adapter – Johny19 Dec 30 '15 at 06:29