7

I'm trying to make the three items of this AlertDialog green. The problem is that currently a green background appears behind the alert, and two of the items do not appear green. I currently set the style of the AlertDialog with this code:

final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.ListRow));

In my styles.xml, I have this style:

<style name="ListRow">
    <item name="android:background">@color/forest_green</item>
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/dialog_text</item>
</style>

enter image description here

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
  • What is your problem with this code anyway? – Basim Sherif May 27 '13 at 04:12
  • Don't use `android:background` on a *theme*. Everything that doesn't define its background will be green. How about you override `android:buttonBarStyle` and `android:buttonBarButtonStyle` instead? – Eugen Pechanec Jun 19 '16 at 18:48
  • You may take a look at these tutorials: http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/ and http://www.materialdoc.com/alerts/ – mbo Jul 28 '17 at 12:20

5 Answers5

0

you can set custom view programmatically like this way..

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();

create dialog_layout.xml in your layouts with green background.

0

this worked to me. all of them have background.

final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.ListRow));

builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("welcome")
        .setMessage("...")
        .setPositiveButton("login", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        }).setNegativeButton("register", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Intent intent = new Intent(getApplicationContext(),RegisterActivity.class);
                startActivity(intent);
            }
        }).show();
Tunaki
  • 132,869
  • 46
  • 340
  • 423
mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
0

Define the dialog style with Theme.AppCompat.Dialog.Alert as parent:

 <style name="AlertDialog" parent="Theme.AppCompat.Dialog.Alert">
    <item name="android:background">@color/green</item>
    ...
</style>

Source: http://www.materialdoc.com/alerts/

mbo
  • 4,611
  • 2
  • 34
  • 54
0

Try this:

    final AlertDialog ad = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppTheme)).create();

add the buttons; and

    for (int i = 0; i < 2;i++) {
        Button b = ad.getButton(i);
        b.setBackgroundColor(color);
    }
    ad.show();

change your theme and color.

-1

Use styles.xml like this

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AlertDialogCustom" parent="@android:style/AlertDialog">
    <item name="android:textColor">#00FF00</item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">10sp</item>
</style>

HardCoder
  • 89
  • 1
  • 1
  • I have use this getting error : error: Error retrieving parent for item: No resource found that matches the given name '@android:style/AlertDialog'. – Anchal Radhwani Oct 01 '14 at 08:37