7

I am trying to style my AlertDialog and I have been able to change most of it through styles and xml declarations... but there are still a few issues:

  1. How do I change the area around the title bar from black to my custom color?
  2. How do I change the outer background to transparent (the outside part that is blue the the shadow drops upon)
  3. How do I change the buttons so they do not overlap the black border around the alert message?

The AlertDialog

here is the function I have in my RootActivity (my activities extend this one)

public static void showNoConnectionDialog(Context ctx1) {
    final Context ctx = ctx1;
    LayoutInflater factory = LayoutInflater.from(ctx);
    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(ctx, R.style.SetdartDialog));
    builder.setView(factory.inflate(R.layout.alert_dialog, null))
    .setIcon(R.drawable.icon)
    .setCancelable(true)
    .setMessage(R.string.check_wireless_settings)
    .setTitle(R.string.no_connection)
    .setPositiveButton(R.string.myes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
        }
    })
    .setNegativeButton(R.string.mno, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            return;
        }
    })
    .setOnCancelListener(new DialogInterface.OnCancelListener() {
        public void onCancel(DialogInterface dialog) {
            return;
        }
    })
    .show();
}

here a snippet from styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.WhiteBackground" parent="android:Theme">
        <item name="android:windowBackground">@null</item>
        <item name="android:background">@android:color/white</item>
        <!-- Dialog attributes
        <item name="alertDialogStyle">@style/AlertDialog</item>  -->
    </style>
    <style name="SetdartDialog">
            <item name="android:background">@color/sd_blue</item> <!-- MUST HAVE with white bg-->
            <!--<item name="android:windowBackground">@color/sd_blue</item> -->
            <!--<item name="android:windowBackground">@color/transparent</item> needed with white bg ? -->
            <item name="android:windowFrame">@color/transparent</item><!-- not sure what this changes-->
            <item name="android:textColor">@android:color/black</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:textSize">10sp</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowContentOverlay">@color/transparent</item>
            <item name="android:windowTitleStyle">@style/setwindowTitleStyle</item>
            <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
            <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
            <item name="android:backgroundDimEnabled">true</item>
            <item name="android:gravity">center_vertical|center_horizontal</item>
        <!--<item name="android:colorBackgroundCacheHint">@android:color/white</item>-->
        </style>
        <style name="setwindowTitleStyle">
            <item name="android:textColor">@android:color/white</item>
            <item name="android:background">@color/sd_blue</item>
        </style>
</resources>

Also R.layout.alert_dialog

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>
</ScrollView>
Traveling_Monk
  • 778
  • 2
  • 11
  • 28

2 Answers2

6

Create your custom layout with all these attributes you've mentioned. Use Dialog instead of AlertDialog, inflate the layout you have created, use the Dialog object to set the inflated layout. If you haven't been introduced to inflating service, do some research. After you are clear with inflating, remember that all the components of the dialog you access with the View object, that you have created with the inflating. The rest (like click listeners) remains to be done on usual way. Cheers. I hope it helps.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • Is this a limitation of the AlertDialog class or a bug? seems as though i can style everything else except the issues i mentioned, I can see their id's in the Hierarchy Viewer is it possible to manipulate those programatically? Your right i don't know much about inflating views, can someone suggest resources to grok this concept other than RTFM? – Traveling_Monk Sep 05 '11 at 09:47
  • I have found several sources for learning about inflating...thx. so is my issue a bug or undocumented feature? :) – Traveling_Monk Sep 05 '11 at 10:32
  • Its limitation of AD. Of course in both AD and D you can manipulate component views. Your features you've mentioned are not documented, since they are custom, only methods and ways are documented, like inflating, accessing children views, etc etc...Cheers – Nikola Despotoski Sep 05 '11 at 11:04
  • 1
    for anyone following this, here are 2 resources I found i liked http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater/5027921#5027921 http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/ – Traveling_Monk Sep 06 '11 at 06:26
0

To make custom AlertDialog you should extend DialogFragment

Rafael
  • 6,091
  • 5
  • 54
  • 79