Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textview: using the dialog title style: @android:style/TextAppearance.DialogWindowTitle

Tags:

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

like image 731
Waza_Be Avatar asked Jan 24 '13 01:01

Waza_Be


2 Answers

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

like image 103
Sherif elKhatib Avatar answered Oct 02 '22 12:10

Sherif elKhatib


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.

like image 31
Shreyash Mahajan Avatar answered Oct 02 '22 12:10

Shreyash Mahajan