-2

How do I set the color of a an android app's dialog's title bar (currently blue, seen in snapshot) at runtime i.e. the color is received inside the java code at runtime?

Snapshot of a full-screen dialog

Shine
  • 3,788
  • 1
  • 36
  • 59
prashant
  • 348
  • 3
  • 13
  • @James color is not predefined, as I said, so I cannot specify any color in XML – prashant Sep 22 '15 at 09:32
  • How are you creating the dialog? Your issue is not clear enough. please add more details. If you just want to know `how to set the color of a dialog pragmatically` then this question is a duplicate like @James said. – Emil Sep 22 '15 at 09:34
  • This is not duplicate. The word "Dialog" has fooled anyone, this has nothing to do with `android.app.Dialog` – Shine Sep 22 '15 at 09:51

3 Answers3

2

To set title color of dialog:

m_dialog = new Dialog(this); // your dialog
m_dialog.getWindow().setTitleColor(R.color.pink_background);

Or you can even set drawable:

m_dailog.getWindow().setBackgroundDrawableResource(R.drawable.your_drawable);

EDIT According to your comment, if you don't have color pre-defined than you need to do something like this:

Example:

m_tvMessage.setTextColor(Color.parseColor("#AA0000"));
0

try this,

final Dialog dailog = new Dialog(MainActivity.this);
dailog.getWindow().setBackgroundDrawableResource(R.drawable.dailogbox);

put dailogbox.xml in your resource/drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

    <gradient
        android:angle="-90"
        android:centerColor="#660D1E4A"
        android:endColor="#66011444"
        android:startColor="#66505E7F"
        android:type="linear"
         />

    <stroke
        android:dashGap="0dp"
        android:dashWidth="0dp"
        android:width="1dp"
        android:color="#ffffffff" />

</shape>
Ganpat Kaliya
  • 888
  • 2
  • 9
  • 16
  • in this, where do I get the functionality of passing a color (coming from another source hence it is unknown beforehand) at runtime? – prashant Sep 22 '15 at 11:21
0

Try below code for changing color of your dialog programatically

    dialog = new Dialog(this); // your dialog
    dialog.getWindow().setTitleColor(R.color.blue_background);
KishuDroid
  • 5,411
  • 4
  • 30
  • 47