Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Appcompat dialog title and title divider color?

Is there any way to change Appcompat dialog title and title divider color? I don't want to use holo light blue color.

I founded this link but is for holo light and don't work with appcompat.

Thanks in advance.

like image 672
Vanama Avatar asked Apr 11 '14 22:04

Vanama


1 Answers

The only way to change the Dialog title divider color is by subclassing Dialog and using Resources.getIdentifier to find the title divider View. After that all you need is a call to View.setBackgroundColor. Since this is the only way to customize the title divider, you may as well go ahead and use the same method to customize the title color as well.

But as far as why you can't get the answer you linked to working for you, it's hard to say. You don't include any code or anything you've tried, so that makes it tricky to pinpoint why you aren't receiving the results you want.

Here's an example of changing the title color and the title divider color:

/**
 * A sublcass of {@link AlertDialog} used to customize the title and title
 * divider colors
 */
public class CustomDialog extends AlertDialog {

    /**
     * Constructor for <code>CustomDialog</code>
     * 
     * @param context The {@link Context} to use
     */
    public CustomDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Resources res = getContext().getResources();
        final int yellow = res.getColor(android.R.color.holo_orange_light);

        // Title
        final int titleId = res.getIdentifier("alertTitle", "id", "android");
        final View title = findViewById(titleId);
        if (title != null) {
            ((TextView) title).setTextColor(yellow);
        }

        // Title divider
        final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
        final View titleDivider = findViewById(titleDividerId);
        if (titleDivider != null) {
            titleDivider.setBackgroundColor(yellow);
        }
    }

}

Implementation

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final CustomDialog customDialog = new CustomDialog(this);
    customDialog.setTitle("Title");
    customDialog.setMessage("Message");
    customDialog.show();
}

Using a DialogFragment with AlertDialog.Builder

public class CustomDialogFragment extends DialogFragment {

    /**
     * Empty constructor as per the {@link Fragment} docs
     */
    public CustomDialogFragment() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                .setTitle("Title")
                .setMessage("Message")
                .create();
    }

    @Override
    public void onStart() {
        super.onStart();
        final Resources res = getResources();
        final int yellow = res.getColor(android.R.color.holo_orange_light);

        // Title
        final int titleId = res.getIdentifier("alertTitle", "id", "android");
        final View title = getDialog().findViewById(titleId);
        if (title != null) {
            ((TextView) title).setTextColor(yellow);
        }

        // Title divider
        final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
        final View titleDivider = getDialog().findViewById(titleDividerId);
        if (titleDivider != null) {
            titleDivider.setBackgroundColor(yellow);
        }
    }

}

Implementation

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new CustomDialogFragment().show(getFragmentManager(), "customDialogFragment");
}

Results

Example

like image 76
adneal Avatar answered Oct 22 '22 09:10

adneal