Ullas
Ullas

Reputation: 11556

Error while creating custom Alert Dialog in android

I am getting the following error while creating a custom Alert Dialog in Android application.

Error

requestFeature() must be called before adding content

Following is my code for the custom Alert Dialog creation.

Code

AlertDialog alertDialog=new AlertDialog.Builder(home.this).create();
alertDialog.setTitle("Title here..");
alertDialog.setContentView(R.layout.custom_alertdialog);
alertDialog.show(); 

Upvotes: 0

Views: 336

Answers (4)

Batuhan Coşkun
Batuhan Coşkun

Reputation: 2969

Use this as guide:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    YourActivity.this);

alertDialogBuilder.setTitle("Your title here...");

alertDialogBuilder
        .setMessage("Your message here...")
        .setCancelable(false)
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // Your works
                }
            })
        .setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                dialog.cancel();
            }
        });

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setContentView(R.layout.custom_alertdialog);
alertDialog.show();

Upvotes: 0

lazy lord
lazy lord

Reputation: 173

Try this :

AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(home.this);
alertDialogBuilder.setTitle("Title here..");
alertDialogBuilder.setContentView(R.layout.custom_alertdialog);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show(); 

Upvotes: 0

Yashdeep Patel
Yashdeep Patel

Reputation: 3140

Code snippet here for custom Dialog:

use new Dialog instead of DialogBuilder

Dialog d = new Dialog(MainActivity.this);
        d.setContentView(R.layout.dialog);
        d.setTitle("This is custom dialog box");
        d.show();   

Upvotes: 2

Ankit Sharma
Ankit Sharma

Reputation: 1251

Remove .create() from first line.

Upvotes: 1

Related Questions