How to show Cancel and Accept button in AlertDialog Android

-1

I am having troubles setting the two buttons on the lower part of the alert what I am trying to do is get a similar look to the IOS look of the buttons. here is my current code:

public void openInstructions() {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    final AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            setNewDateOnView();
            dialog.cancel();
        }
    }).setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    }).create();

    TextView myMsg = new TextView(getActivity());
    myMsg.setText("ExampleText ");
    myMsg.setTextSize(15);
    myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
    dialog.setView(myMsg);

    dialog.setTitle("Confirm Date of Purchase");

    dialog.setOnShowListener( new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface arg0) {
            Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            negativeButton.setTextColor(0xFFFF0000);
            Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positiveButton.setTextColor(0xFF0000FF);
        }
    });
    dialog.show();

}

Example of how it looks right now:

enter image description here

How I wanted it to look:

enter image description here

java
android
interface

4 Answers

1

Well, as a start, google recommends you use the Android Design Guidelines when creating Android apps, really you should be sticking to those as that's what your users will be used to (not Apple's).

Also there's no reason to be assigning the dialog a TextView if all you're doing is showing text, the AlertDialog provides this functionality with setMessage.

If that hasn't convinced you to stick to the normal design rules, the only other option would be creating a Custom View and assign it to the AlertDialog with setView(similar to how you're doing it with the TextView).

Here's a tutorial on creating a Custom View

answered on Stack Overflow Aug 27, 2019 by Zintom
1

You can create a custom view such as the one sent and inflate it

Example:

                   AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
                   View mView=getActivity().getLayoutInflater().inflate(R.layout.custom_layout, null);
                    mBuilder.setView(mView);
                    Button close = (Button) mView.findViewById(R.id.close);
                    Button OK = (Button) mView.findViewById(R.id.ok);

                    close.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                         //close the dialog
                            hideDialog1();
                        }
                    });

                    OK.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                         //do something
                        }
                    });

                    dialog = mBuilder.create();
                    dialog.setCanceledOnTouchOutside(false);
                    dialog.show();
1

Use .setNeutralButton that make place left most

TextView textview; Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button =(Button)findViewById(R.id.button1);
    textview = (TextView)findViewById(R.id.textView1);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.ic_launcher_background)

                    .setTitle("Alert Dialog Box Title")

                    .setMessage("Are you sure( Alert Dialog Message )")

                    .setPositiveButton("YES", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            Toast.makeText(MainActivity.this, "You Clicked on Yes", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            Toast.makeText(MainActivity.this, "You Clicked on Cancel", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setNeutralButton("NO", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            Toast.makeText(MainActivity.this, "You Clicked on NO", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .show();
        }
    });

}

===============================================

answered on Stack Overflow Aug 28, 2019 by Suman Kumar Dash
1

To design Dialog like ios , we can create custom dialog using custom layout like this :

dialog_custom.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:orientation="vertical"
    app:layout_constraintHeight_max="wrap">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/custom_bg">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@+id/title"
            android:gravity="center"
            android:text="Confirm Date of Purchase"
            android:textAlignment="center"
            android:textColor="@android:color/black"
            android:textSize="17sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/back_ll"
            android:orientation="vertical"
            android:padding="5dp">

            <TextView
                android:id="@+id/rebooking_single_tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                android:textAlignment="center"
                android:text="@string/dummuy"
                android:textColor="@android:color/black"
                android:textSize="17sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/back_ll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/end_time_recycle"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="#8F8F8F" />
            <LinearLayout
                android:id="@+id/rebooking_back_button_ll"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:orientation="horizontal"
                android:gravity="center">

                <TextView
                    android:id="@+id/ok_btn"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight=".49"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:text="Ok"
                    android:textAlignment="center"
                    android:textColor="#FF6363"
                    android:textSize="17sp"
                    android:textStyle="bold" />
                <View
                    android:layout_width="0dp"
                    android:layout_weight=".01"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/end_time_recycle"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="#8F8F8F" />
                <TextView
                    android:id="@+id/cancel_btn"
                    android:layout_width="0dp"
                    android:layout_weight=".5"
                    android:layout_height="wrap_content"
                    android:paddingLeft="10dp"
                    android:textAlignment="center"
                    android:paddingRight="10dp"
                    android:text="Cancle"
                    android:textColor="#7BC5FF"
                    android:textSize="17sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

Main_Activity

final Dialog dialog=new Dialog(FirstActivity.this);
  dialog.setCancelable(false);
  dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  dialog.setContentView(R.layout.dialog_custom);
  TextView cancel_btn=dialog.findViewById(R.id.cancel_btn);
  cancel_btn.setOnClickListener(new View.OnClickListener(){
     @Override
     public void onClick(View view){
        dialog.dismiss();
     }
  });
  TextView ok_btn=dialog.findViewById(R.id.ok_btn);
  ok_btn.setOnClickListener(new View.OnClickListener(){
     @Override
     public void onClick(View view){
        dialog.dismiss();

     }
  });
  dialog.show();

enter image description here

answered on Stack Overflow Aug 28, 2019 by Android Geek

User contributions licensed under CC BY-SA 3.0