Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the blue line in the AlertDialog ( Holo Theme )

Tags:

java

android

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>
like image 879
g.revolution Avatar asked Sep 05 '12 06:09

g.revolution


2 Answers

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.

like image 37
Chris Avatar answered Oct 07 '22 06:10

Chris


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...

like image 102
Daniel Smith Avatar answered Oct 07 '22 06:10

Daniel Smith