Jensen
Jensen

Reputation: 85

how to change the alertdialog box style in android

I am just trying to build a alertdialog for my android application.

what I actually want is a dialog box like this: enter image description here

whose positive and negative button are filled across dialog box

but I always get something like this:

enter image description here

and these two buttons stick together at the right corner of dialog box

can anybody tell me what to do to change the alertdialog from the second one to first one?

many thanks

Upvotes: 2

Views: 613

Answers (2)

Srikanth
Srikanth

Reputation: 1575

You can set customized layout to alert dialog by using setView(view) method like this:

 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
 LayoutInflater inflater = this.getLayoutInflater();
 View dialogView = inflater.inflate(R.layout.alert_layout, null);
 dialogBuilder.setView(dialogView);
 AlertDialog alertDialog = dialogBuilder.create();
 alertDialog.show();

Upvotes: 0

Narendra Motwani
Narendra Motwani

Reputation: 1115

For that You need to create custom dilogbox Like this Dialog xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/image"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:contentDescription="@drawable/ic_launcher"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/button"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/image"
    android:layout_centerHorizontal="true"
    android:text="Dismiss" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_above="@+id/button"
    android:layout_toRightOf="@+id/image"
    android:gravity="center_vertical"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

And In your Activity :

final Dialog dialog = new Dialog(MainActivity.this);

            //setting custom layout to dialog
            dialog.setContentView(R.layout.cusotm_dialog_layout);
            dialog.setTitle("Custom Dialog");

            //adding text dynamically
            TextView txt = (TextView) dialog.findViewById(R.id.textView);
            txt.setText("Put your dialog text here.");

            ImageView image = (ImageView)dialog.findViewById(R.id.image);
            image.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_info));

            //adding button click event
            Button dismissButton = (Button) dialog.findViewById(R.id.button);
            dismissButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();

Upvotes: 1

Related Questions