how to include custom title view with in AlertDialog in android?
Asked Answered
F

2

19

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

Firm answered 24/2, 2012 at 11:47 Comment(4)
This will show,how you create a custom dialog developer.android.com/guide/topics/ui/dialogs.html#CustomDialogOverseer
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 thanksFirm
not working means? is it giving some error? or simply not showing the custom title? i think the code is correct it should work.Schweiz
@Schweiz its showing default title bar.Firm
S
58

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();
Schweiz answered 24/2, 2012 at 12:33 Comment(1)
getLayoutInflater() crashes my fragment in onCreateDialog. LayoutInflater.from(getActivity()) works.Leucite
C
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

Curfew answered 24/2, 2012 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.