7

I want to change the color of the blue Title and blue line that comes in AlertDialog ( Holo Theme ), I was able to change the color of Title by looking into android styles.xml and themes.xml but i cannot find any property or style that i can use/change to change the color of my XML.

Following is the style that I am using to change the title color

<style name="myAlertDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowTitleStyle">@style/myAlertDialogTitleStyle</item>
</style>

<style name="myAlertDialogTitleStyle">
    <item name="android:maxLines">1</item>
    <item name="android:scrollHorizontally">true</item>
    <item name="android:textAppearance">@style/myAlertDialogTitleStyleTextAppearance</item>
</style>

<style name="myAlertDialogTitleStyleTextAppearance">
    <item name="android:textSize">22sp</item>
    <item name="android:textColor">#ffff8800</item>
</style>
g.revolution
  • 11,962
  • 23
  • 81
  • 107

3 Answers3

4

EDIT: I was mistaken in my original answer.

Unfortunately the styles used for dialogs are internal and cannot be used in the same manner as some of the other easy-to-manipulate holo elements. I have created an open source project for dealing with this in very simple situations (I hope to extend it down the road and make it more.. legit). Here is a link to a new answer that addresses this more in depth: How can I change the color of AlertDialog title and the color of the line under it


For posterity, here is my naive old answer:

Look at the answer posted here. For your particular goal, you will want to be overriding the dialogTitleDecorLayout style (I think!) as opposed to the listSeparatorTextViewStyle style.

If you are curious where this is originally set, you can poke around the themes.xml file in your android sdk folder on your computer and search for the dialogTitleDecorLayout attribute. This should lead you to a dialog_title_holo layout file which should also be on your computer. There you should find the following code within the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true">
  <TextView android:id="@android:id/title" style="?android:attr/windowTitleStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="60dip"
    android:paddingLeft="32dip"
    android:paddingRight="32dip"
    android:gravity="center_vertical|left" />
  <ImageView android:id="@+id/titleDivider"
        android:layout_width="match_parent"
        android:layout_height="4dip"
        android:scaleType="fitXY"
        android:gravity="fill_horizontal"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:src="@android:drawable/divider_strong_holo" />
  <FrameLayout
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:foreground="?android:attr/windowContentOverlay">
    <FrameLayout android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  </FrameLayout>
</LinearLayout>

The code of note is the @android:drawable/divider_strong_holo reference. This file also exists on your computer and it should be a blue 9patch. You will need to create a similar 9patch of a different color. Good Luck! Even if this isn't quite the right attribute I think you see what you may need to do here...

Community
  • 1
  • 1
Daniel Smith
  • 8,561
  • 3
  • 35
  • 58
  • I didn't know if your answer solve this http://stackoverflow.com/questions/14439538/how-can-i-change-the-color-of-alertdialog-title-and-the-color-of-the-line-under – Mohammed Subhi Sheikh Quroush Mar 07 '13 at 09:25
  • @forsubhi I think it does solve that issue, but I would like to test it out on my own before posting an answer just to make 100% sure. (unless you already tried? In which case I would be happy to post my answer) – Daniel Smith Mar 07 '13 at 10:24
  • @forsubhi I have answered your question and open sourced some potentially helpful code. Let me know if you need help figuring out how to use my code. – Daniel Smith Mar 08 '13 at 02:21
4

After some tinkering, I've come up with a way to re-brand the dialog the moment it is shown:

private AlertDialog showWithThemeColors() {
    Log.d(TAG, "showWithThemeColors()");
    AlertDialog dialog = this.builder.show();
    try {
        ViewGroup decorView = (ViewGroup) dialog.getWindow().getDecorView();
        FrameLayout windowContentView = (FrameLayout) decorView.getChildAt(0);
        FrameLayout contentView = (FrameLayout) windowContentView.getChildAt(0);
        LinearLayout parentPanel = (LinearLayout) contentView.getChildAt(0);
        LinearLayout topPanel = (LinearLayout) parentPanel.getChildAt(0);
        View titleDivider = topPanel.getChildAt(2);
        LinearLayout titleTemplate = (LinearLayout) topPanel.getChildAt(1);
        TextView alertTitle = (TextView) titleTemplate.getChildAt(1);

        int textColor = this.context.getResources().getColor(R.color.customer_text_on_primary);
        alertTitle.setTextColor(textColor);

        int primaryColor = this.context.getResources().getColor(R.color.customer_primary);
        titleDivider.setBackgroundColor(primaryColor);
    } catch (Exception e) {
        Log.e(TAG, "showWithThemeColors() - Could not re-brand dialog with theme colors.", e);
    }

    return dialog;
}

Note, that this solution can break anytime the underlying system layout for the dialog changes. However, it is the original system dialog with all the advantages of such a solution.

Chris
  • 3,192
  • 4
  • 30
  • 43
  • It's a hack, but a functional hack. Not sure what type of performance impact this adds to an app? Going to test it and see how it works over the long haul. Thanks for the effort! – Kyle Nov 05 '13 at 13:17
1

There is a library which does exactly that for all your dialogs:

https://github.com/inmite/android-styled-dialogs

David Vávra
  • 18,446
  • 7
  • 48
  • 56
  • 1
    Hi, thanks for the pointer. Actually, we tried using this library in our project, but dropped it again. At least at the time it fell short in replacing the native API, and could not accomodate all kinds of dialogs we had already built. – Chris Nov 08 '13 at 12:20