17

how can i include custom titlebar in alertDialog?I know android sdk provide setCustomTitle method but it does'not work

edit:

    AlertDialog alert = new AlertDialog.Builder(this).setTitle("Test").setMessage("hello").show();
    View view=alert.getLayoutInflater().inflate(R.layout.titlebar, null);
    alert.setCustomTitle(view);

but above code is not working

NOTE : I am not looking for custom dialog box but only want to makes its title layout custom..Like belowlike this with close Button

sAaNu
  • 1,428
  • 7
  • 21
  • 46
  • 1
    This will show,how you create a custom dialog http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog – Vinayak Bevinakatti Feb 24 '12 at 11:54
  • I know how to create custom dialogbox but I dont want any custom dialogbox but only need custum title,so that i can add button to its title, anyway thanks – sAaNu Feb 24 '12 at 11:55
  • not working means? is it giving some error? or simply not showing the custom title? i think the code is correct it should work. – Vikram Feb 24 '12 at 12:13
  • @Vikram its showing default title bar. – sAaNu Feb 24 '12 at 12:18

2 Answers2

57

you should not use the .show method with the first line, use the following code it works:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.titlebar, null);
alert.setCustomTitle(view);
alert.setMessage("helo");
alert.show();
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Vikram
  • 8,235
  • 33
  • 47
0
    DisplayDialog d = new DisplayDialog(this);
    d.show();

    public class DisplayDialog extends Dialog implements android.view.View.OnClickListener
    {
        public DisplayDialog(Context c) 
        {
            super(c, R.style.Theme_Dialog_Translucent);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);

            setCanceledOnTouchOutside(false);
            setContentView(R.layout.your_layout);
        }
    }

Add this snippet to your strings.xml

 <style name="Theme_Dialog_Translucent" parent="android:Theme.Dialog">
     <item name = "android:windowBackground">@color/transparent</item>
 </style>
 <color name="transparent">#00000000</color> 

Use dismiss(); to close the dialog

xarlymg89
  • 2,552
  • 2
  • 27
  • 41
KK_07k11A0585
  • 2,381
  • 3
  • 26
  • 33