pippo15
pippo15

Reputation: 113

How to add the fourth button?

I made an alert box that have a title, a description for every option and three button. Now I saw that I need another button! But I saw that exist only negative, neutral and positive button. This is my code:

alertDialog = new AlertDialog.Builder(Home.this);
        LayoutInflater inflater = this.getLayoutInflater();
        alertDialog.setView(inflater.inflate(R.layout.alert_dialog_layout, null));


        alertDialog.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //do a thing
            }
        });

        alertDialog.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //do a thing
            }
        });

        alertDialog.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //do a thing
            }
        });

        alertDialog.show();

And alert_dialog_layout.xml is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="@color/white"
        android:background="@drawable/button_blue" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/padding_and_margin"
        android:text="@string/text4"
        android:gravity="center" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/padding_and_margin"
        android:gravity="center"
        android:text="@string/text5" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/padding_and_margin"
        android:gravity="center"
        android:text="@string/text6"
        android:layout_marginBottom="@dimen/padding_and_margin" />
</LinearLayout>

I want another button because I have 4 option. Is possible to use alert button or I have change the view? Is possible to change the layout of the 3 (or 4) button that I automatically made? Thanks for answer

Upvotes: 0

Views: 1320

Answers (2)

King of Masses
King of Masses

Reputation: 18765

You have to create custom layout.

http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

Create your own layout and Inflate it to get the View and follow the link for the rest.

Upvotes: 0

Jitender Dev
Jitender Dev

Reputation: 6925

Inflate the AlertDialog with your own custom view (alert_dialog_layout.xml) and place the buttons in your alert_dialog_layout.xml

AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
//inflate view for alertdialog since we are using multiple views inside a viewgroup (root = Layout top-level) (linear, relative, framelayout etc..)
View view = inflater.inflate(R.layout.alert_dialog_layout, (ViewGroup) findViewById(R.id.root)); 

Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4.
alert.setView(view);
alert.show();

Upvotes: 1

Related Questions