0

ok, I am making an app for a phone. I made a function for my dialog and just calling the function when needed.

I want to be able to change the dialog window ie: different colors and lines and stuff to separate the text out.

I have looked all over the internet and I cant find anything that can help me.

here is the function that I am calling....

public void userNameRequired(View view)
{
    AlertDialog.Builder builder = new AlerdDialog.Builder(this);
    TextView newMessage = new TextView(this);
    newMessage.setText(" User Name is Required to be between 7 and 20    caracters long.");
    newMesage.setGravity(Gravity.CENTER_HORIZONTAL);
    builder.setView(newMessage);

    TextView title = new TextView(this);
    title.setText("!! NOTICE !!");
    title.setGravity(Gravity.CENTER);
    title.setTextSize(28);
    title.setTextColor(Color.RED);

    builder.setCustomTitle(title);

    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int id)
        {

        }
    });

    builder.show();
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

As answered by pha, you can assign a custom theme to the Alert Dialog :

How to change theme for AlertDialog

Alexandre Martin
  • 1,472
  • 5
  • 14
  • 27
0

Solution 1:

  1. Open colors.xml
  2. Define color like this: <color name="background_dark">#3F51B5</color>
  3. After calling show() on your dialog, set the background color like this:

    builder.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
    

Solution 2:

  1. Define a custom theme

    //assuming you have `@color/White` and `@color/colorPrimaryDark` defined in `colors.xml`
    <style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">@color/White</item>
        <item name="android:textStyle">bold</item>
        <item name="android:headerDividersEnabled">true</item>
        <item name="android:typeface">normal</item>
        <item name="android:background">@color/colorPrimaryDark</item>
    </style>
    
  2. set style

    AlertDialog.Builder builder = new AlerdDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
    

So, this is how your code should look like:

    public void userNameRequired(View view)
    {
       AlertDialog.Builder builder = new AlerdDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
       TextView newMessage = new TextView(this);
       newMessage.setText(" User Name is Required to be between 7 and 20    caracters long.");
       newMesage.setGravity(Gravity.CENTER_HORIZONTAL);
       builder.setView(newMessage);

       TextView title = new TextView(this);
       title.setText("!! NOTICE !!");
       title.setGravity(Gravity.CENTER);
       title.setTextSize(28);
       title.setTextColor(Color.RED);

       builder.setCustomTitle(title);

       builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
       {
           public void onClick(DialogInterface dialog, int id)
           {

           }
       });

       builder.show();
    }

AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
        ...
        .create();
Atlas_Gondal
  • 2,512
  • 2
  • 15
  • 25