4

I'm using a single choice alert dialog in which I'd like to replace the default blue (the title line and the radio buttons) to the orange that I am using in the title bar. I was able to change to the title bar using setCustomTitle(), but I am lost trying to get rid of this damn blue.

For the title bar

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/orange" >

    <TextView
        android:id="@+id/alertTitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="14dp"
        android:gravity="center"
        android:text="Alert Title"
        android:textColor="@color/white"
        android:textSize="18sp" />    

</LinearLayout>

Building the AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View customTitle = View.inflate(MainActivity.this, R.layout.custom_alert_title, null);
builder.setCustomTitle(customTitle);
builder.setSingleChoiceItems(mAlertOptions, -1, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {                    
        // do stuff
        dialog.dismiss();
    }
}).create().show();

This is what it looks like

enter image description here

I need to get rid of this blue! Help!

1 Answer 1

11

The only way to change the title divider color is to use Resources.getIdentifier in combination with Window.findViewById. The checkmark can be easily changed by calling AlertDialog.Builder.setSingleChoiceItems(ListAdapter, int, OnClickListener).

Here's an example:

Single choice item layout : I generated your_radio_button using Android Holo Colors.

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkMark="@drawable/your_radio_button"
    android:ellipsize="marquee"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingEnd="16dip"
    android:paddingStart="16dip"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="?android:attr/textColorAlertDialogListItem" />

Implementation

    final ListAdapter adapter = new ArrayAdapter<String>(this,
            R.layout.select_dialog_singlechoice, android.R.id.text1, new String[] {
                    "Option 1", "Option 2"
            });

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCustomTitle(getLayoutInflater().inflate(R.layout.custom_alert_title, null));
    builder.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Do something
        }
    });

    // Show the AlertDialog
    final AlertDialog dialog = builder.show();

    // Change the title divider
    final Resources res = getResources();
    final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
    final View titleDivider = dialog.findViewById(titleDividerId);
    titleDivider.setBackgroundColor(res.getColor(android.R.color.holo_orange_dark));

Results

Example

2
  • 1
    This is perfect! Thank you so much! One last thing, do you know how to get rid of the blue highlight that flashes when the row is clicked? That seems to be the last of the blue.
    – Kevin_TA
    Apr 24, 2014 at 15:00
  • @Kelvin_TA add this attribute to the checkedTextView android: background="?android:attr/selectableItemBackground. But remember that you must set clickable and focusable attribute to true for it to work by adding android:clickable="true". android:focusable="true" Sep 6, 2022 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.