Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set AlertBox Title Bar Background Color

How can I change the background color for an alertbox's title bar?

AlertDialog.Builder alert=new AlertDialog.Builder(getParent());
alert.setTitle("sample");
alert.show();
like image 235
Jeeva Avatar asked May 09 '11 05:05

Jeeva


1 Answers

The easiest way is to subclass a dialog by creating a class which extends dialog and implements the constructor which take style as a parameter. Then make your own custom layout to it.

The Code to show the dialog:

private void showDialog()
{
    Custom_Dialog dialog = new Custom_Dialog(this, R.style.myCoolDialog);

    dialog.setContentView(R.layout.custom_dialog);
    dialog.setTitle("Custom Dialog");

    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("Hello, this is a custom dialog!");
    ImageView image = (ImageView) dialog.findViewById(R.id.image);
    image.setImageResource(R.drawable.icon);

    dialog.show();  
}

The code for the subclass:

package com.stackoverflow;

import android.app.Dialog;
import android.content.Context;

public class Custom_Dialog extends Dialog {

    protected Custom_Dialog(Context context, int theme) {
        super(context, theme);
        // TODO Auto-generated constructor stub
    }

}

the style: myCoolDialog.xml

<resources>
    <style name="myCoolDialog" parent="android:Theme.Dialog"> 
        <item name="android:windowBackground">@drawable/blue</item>
        <item name="android:colorForeground">#f0f0</item> 
     </style> 
</resources>

and last the layout:custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>
like image 75
CornflakesDK Avatar answered Oct 23 '22 14:10

CornflakesDK