ElliotM ElliotM - 1 year ago 211
Android Question

Theme.AppCompat.Dialog title padding issues.

When using

as the theme for my DialogFragment, I'm unable to apply a padding to the entire title view. I can successfully apply
to the text of the title, but the icon is ignored.
Icon is applied via
getDialog().setFeatureDrawable(Window.FEATURE_LEFT_ICON, drawable);

DialogFragment style

<style name="my_dialog" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">@color/NavBarTint</item>
<item name="android:windowTitleStyle">@style/DialogWindowTitle</item>
<item name="android:windowNoTitle">false</item>

<!-- Doesn't apply to icon -->
<item name="drawablePadding">8dp</item>

<item name="colorAccent">@color/NavBarTint</item>

<style name="DialogWindowTitle">
<!-- Applies to only the text -->
<item name="android:paddingBottom">8dp</item>

<item name="android:textColor">@color/DialogTitleText</item>
<item name="android:textSize">22sp</item>
<item name="android:textColorPrimary">@color/DialogTitleText</item>

Results in

enter image description here

I know an alternative would be to scrap the title and make your own but we have a lot of dialogs that all inherit from this style and would love to know the root of the issue than a implement a work around.


Answer Source

Had to apply the a hack for post lollipop devices (eventually removed icons all together) but I'll post what we used.

 * This method can be called to add padding
 * below an icon if used. New appcompat library introduced
 * bug where padding below icon is ignored.
public void applyAppCompatDialogIconPaddingFix() {
    if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) && (getView() instanceof ViewGroup)) {
        ViewGroup rootView = (ViewGroup) getView();
        if (rootView instanceof ScrollView) {
            //going to safely assume a child exists...
            rootView = (ViewGroup) rootView.getChildAt(0);
            if (rootView == null) {

        float padding = ContextUtil.pxFromDp(getContext(), 12f);

        View paddingFix = new View(getActivity());
        paddingFix.setBackgroundResource(R.color.NavBarTint); //same color as title

        if (rootView instanceof LinearLayout) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, (int) padding);
        } else if (rootView instanceof RelativeLayout) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, (int) padding);
        } else {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, (int) padding);

        rootView.addView(paddingFix, 0);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download