wingsraam
wingsraam

Reputation: 187

How to set color or highlight PositiveButton and NegativeButton in alert dialog box and customized

I am searching for set color in the alert dialog box positive button. But I didn't get any solution, so please suggest me any one good solution for the alert dialog box.

My code is here

delete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub  
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setTitle("Delete ");
        alertDialogBuilder
        .setMessage("Are you sure, you want to delete ")
        .setCancelable(false)
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {                    

                datab=mdb.getWritableDatabase();
                datab.execSQL("DELETE FROM demo WHERE student_id='"+getid+"'");
                Toast.makeText(getApplicationContext(), "Successfully deleted", 10).show();
                Intent i=new Intent(StudentInfo.this,MainActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(i);                               }
        })
        .setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                dialog.cancel();
            }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
});

I want to set RED color for Yes Button in the alert dialog box.

Upvotes: 2

Views: 1099

Answers (2)

yennsarah
yennsarah

Reputation: 5517

You can get the Buttons via:

alertdialog.show();
//possible Buttons:  BUTTON_NEGATIVE, BUTTON_NEUTRAL, BUTTON_NEUTRAL
Button myPositiveButton = alertdialog.getButton(AlertDialog.BUTTON_POSITIVE);
//pseudo code for now:
myPositiveButton.setOnClickListener(...);
myPositiveButton.setBackgroundColor(...);

Upvotes: 0

Android
Android

Reputation: 535

Just simple one! Create a dialog method something like this anywhere in your Java class.

public void openDialog() {
    final Dialog dialog = new Dialog(context); // context, this etc.
    dialog.setContentView(R.layout.dialog_demo);
    dialog.setTitle(R.string.dialog_title);
    dialog.show();
}

Now create Layout XML dialog_demo.xml and create your UI/Design. Here is a sample one I created for demo purpose.

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

    <TextView
        android:id="@+id/dialog_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/dialog_text"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/dialog_info">

        <Button
            android:id="@+id/dialog_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="@color/dialog_cancel_bgcolor"
            android:text="Cancel"/>

        <Button
            android:id="@+id/dialog_ok"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="@color/dialog_ok_bgcolor"
            android:text="Agree"/>
    </LinearLayout>
</RelativeLayout>

Now you can call openDialog() from anywhere you like :) Here is the screenshot of above code enter image description here
Note that text and color are used from strings.xml and colors.xml. You can define your own. Hope this is helpful. Thanks!

other wise you can use this code it works for me

    public void createDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do you want to exit from app");
    builder.setCancelable(false);
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "You exit from app",
                    Toast.LENGTH_LONG).show();

        }
    });

    AlertDialog alert = builder.create();
    alert.show();
    Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setBackgroundColor(Color.MAGENTA);
    Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
    pbutton.setBackgroundColor(Color.YELLOW);
}

Upvotes: 3

Related Questions