Ibrahim Shalhoub
Ibrahim Shalhoub

Reputation: 119

AlertDialog not Presenting correctly the dialog i wanted

i am trying to present an alertdialog in my actvitiy which describe if the user doesnot have some information stored in my database then a alertdialog will popup in front of the user with button. here is my code for the dialog in activity

AlertDialog.Builder myAlert= new AlertDialog.Builder(this);
myAlert.setMessage("You are winner!").setPositiveButton("Continue...", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
})
.setTitle("Welcome")
.create();
myAlert.show();

so basicly i am not getting the desired alertdialog i wanted,i am getting something like this enter image description here.

i have no borders between title and message and button, my button not placed at center as default ,what should i do to fix these i have tried many videos and posts.

Upvotes: 0

Views: 498

Answers (3)

Jas
Jas

Reputation: 3212

Try this:

 AlertDialog.Builder myAlert= new AlertDialog.Builder(this, android.R.style.Theme_Holo_Light_Dialog);

    myAlert.setTitle("Welcome")
.setMessage("You are winner!")
           .setCancelable(false)
           .setPositiveButton("Continue...", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    //do things
               }
           });
    AlertDialog alert = myAlert.create();
    alert.show();

Upvotes: 0

Rahul Chaurasia
Rahul Chaurasia

Reputation: 1641

i have no borders between title and message and button, my button not placed at center as default ,what should i do to fix these i have tried many videos and posts.

In case, if you don't like the material dialog and you want to platform's specific dialog then you should use android.app.AlertDialog in place of android.support.v7.app.AlertDialog.

Change the import statement from android.support.v7.app.AlertDialog to android.app.AlertDialog

But I recommend you go with material dialog box.

Upvotes: 2

Zahan Safallwa
Zahan Safallwa

Reputation: 3914

You should use custom dialog for this purpose. Create a custom layout. And setlayoutview to the dialog.

sample code

button = (Button) findViewById(R.id.buttonShowCustomDialog);

    // add button listener
    button.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View arg0) {

        // custom dialog
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
      }
    });

See details at below link

http://www.mkyong.com/android/android-custom-dialog-example/

Upvotes: 1

Related Questions