20

I am currently trying tu build my own DialogFragment theme.

One of my requirement is to use an icon at the right of the title.

First idea

Simply using:

this.getDialog().getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dialog_title);

But unfortunately, this line of code has no result.

Second idea

Provinding my own layout -this.setContentView()- with this textview:

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="From XML"
    android:textAppearance="@android:style/TextAppearance.DialogWindowTitle" />

This works, because I can read the text, but it still written in default black font.

I was expecting TextAppearance.DialogWindowTitle to give my text a title appareance (blue in Holo with big font size)

Any idea or suggestion?

EDIT

This nearly did the trick:

<style name="Dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowTitleStyle">@style/MyOwnDialogTitle</item>
</style>

<style name="MyOwnDialogTitle">
    <item name="android:drawableRight">@drawable/MyRightImage</item>
</style>

but android:drawableRight just broke the title layout:

enter image description here

SECOND EDIT

This is a little bit better:

<style name="Dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowTitleStyle">@style/MyOwnDialogTitle</item>
</style>

<style name="MyOwnDialogTitle">
    <item name="android:textAppearance">@android:style/TextAppearance.DialogWindowTitle</item>
    <item name="android:drawableRight">@drawable/icon</item>
</style>

enter image description here

Waza_Be
  • 39,407
  • 49
  • 186
  • 260

3 Answers3

19

Maybe you can extend the default style in this manner:

res/values/dialogstyles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowTitleStyle">@style/MyOwnDialogTitle</item>
    </style>
    <style name="MyOwnDialogTitle">
        <item name="android:drawableRight">@drawable/MyRightImage</item>
    </style>
</resources>

res/values-v11/dialogstyles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
     <style name="Dialog" parent="@android:style/Theme.Holo.Dialog">
          <item name="android:windowTitleStyle">@style/MyOwnDialogTitle</item>
     </style>
</resources>

And override your DialogFragment's onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.Dialog);
}

Working Example

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • This is nearly working: see screenshot in edited question: I am still working on it but cannot find what's wrong! – Waza_Be Jan 30 '13 at 12:37
  • oh I know what is wrong! Check the edited answer (Since we are only extending Theme.Dialog, we are not using the new themes. so you should use the right base Theme depending on the api. – Sherif elKhatib Jan 30 '13 at 13:15
  • Note: adding @android:style/TextAppearance.DialogWindowTitle is a little bit closer to the answer.. I guess we are 99% close to the right answer! – Waza_Be Jan 30 '13 at 13:26
  • @Waza_Be no the problem is that we were always extending Theme.Dialog, but we really need the Holo theme for api > 11 so, you should edit the values-v11/dialogstyles.xml and values-v14/dialogstyles.xml to look like the second xml I have in my answer, while the styles/dialogstyles.xml should be like the first one. We do not want to mess with textAppearance, we only want to add a drawable Right to the default dialog theme's title – Sherif elKhatib Jan 30 '13 at 13:35
  • 1
    This work, but I have a gap beween v11 and v14 as Holo.DialogWindowTitle is only available since API 14! But I accepted for your kind help.@android:style/TextAppearance.Holo.DialogWindowTitle – Waza_Be Jan 30 '13 at 13:41
  • @Waza_Be check this project I just created: http://www.mediafire.com/download.php?3hbgnogax2cx12z – Sherif elKhatib Jan 30 '13 at 14:20
  • Wow, having to set `setStyle` in **onCreate** and it no longer works in **onCreateView**. This is magic, thanks for the answer. – EpicPandaForce May 26 '15 at 10:21
1

I am searching for your first requirement. but for second requirement, why dont you try with changing the color to style of the TextAppearance.DialogWindowTitle with your custom style ?

I have just check the android resources where there is style like below:

  <style name="DialogWindowTitle">
    <item name="android:maxLines">1</item>
    <item name="android:scrollHorizontally">true</item>
    <item name="android:textAppearance">@style/TextAppearance.DialogWindowTitle</item>
</style>

and for TextAppearance.DialogWindowTitle style:

 <style name="TextAppearance.DialogWindowTitle">
    <item name="android:textSize">18sp</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">?textColorPrimary</item>
</style>

Now, you can change the color of your TextAppearance.DialogWindowTitle style and do as you want.

Hope you got the point.

Feel free to comment.

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • Because on our beloved Touchwizz and Sense devices, the dialog are quite different. I simply want to add an ImageView in the native DeviceDefault dialog, and maintaining the original dialog style. – Waza_Be Jan 29 '13 at 10:02
0

Have you tried to use @android:style/Theme.Holo.Light.Dialog as your parent in styles? This will work only in Android 3+ because in earlier version there is no theme Holo, but you can create your own to look like it for lower versions of Android.

hardartcore
  • 16,886
  • 12
  • 75
  • 101
  • That's indeed what I did (otherwise the dialog would be dark ;-) ) – Waza_Be Jan 30 '13 at 13:06
  • So did you tried to set in your custom theme a title color for your dialog? – hardartcore Jan 30 '13 at 13:26
  • No, the code is shared in the post, you have the style.xml code and no custom theme ( parent="@android:style/Theme.Dialog") // Theme.Light.Holo.Dialog in v11 – Waza_Be Jan 30 '13 at 13:31
  • So instead of setting `android:textAppearance` option why don't you try to set the size/color and etc of the title yourself? – hardartcore Jan 30 '13 at 13:40
  • Because I want to keep the custom style of manufacturers. TextAppearance.DeviceDefault.DialogWindowTitle was the correct answer. – Waza_Be Jan 30 '13 at 13:46
  • The "corrected answer" isn't anywhere in this page, except in that comment. Maybe you could post your final version. – Henrique de Sousa Oct 03 '14 at 10:39