0

By default, the buttons of the dialog are aligned to the right like this:

enter image description here

Not sure if this is recommended standard by Android, but to me it looks weird. I would like to center the buttons instead.

I have tried the different configurations from here without results: align AlertDialog buttons to center

Perhaps its the java that confuses me when I try writing it in kotlin or that the post is 3.5 years old. Anyway, does someone have any suggestions on how to solve this programatically without resorting to xml and inflating the dialog?

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
tore
  • 619
  • 2
  • 9
  • 23
  • Yes, if you don't set anything special yourself, you get the recommended / platform default layout which can also be different on other versions of android. I'd not mess with this. – zapl Oct 10 '18 at 10:20

1 Answers1

0

If you do not like the Alert Dialog builder method here is a easy custom Alert Dialog

This btnDeleteDept calls the doCustom() fun

        btnDeleteDept.setOnClickListener {
        if (etDeptData.text.toString().equals("")) {
            message("No Match Found")
            return@setOnClickListener
        }
        doCustom()
    }

Here is the code for the doCustom() fun it uses another fun as you can see

    fun doCustom() {
    /* This method uses the custom_dialog.xml file created for greater control over
   the styling of the Custom Alert Dialog for various screen sizes and to be
   able to set the text size of the dialog message text
   */
    val makeDialog = LayoutInflater.from(this).inflate(R.layout.custom_dialog,null)
    val mBuilder = AlertDialog.Builder(this).setView(makeDialog)
    val mAlertDialog = mBuilder.show()

    val btnYES = makeDialog.findViewById<Button>(R.id.btnYES)
    val btnNO = makeDialog.findViewById<Button>(R.id.btnNO)
    mAlertDialog.setCancelable(false)

    btnYES.setOnClickListener {
        removeDEPT()
        mAlertDialog.dismiss()
    }

    btnNO.setOnClickListener {
        message("Record NOT Deleted")
        etDeptData.setText("")
        //Timer().schedule(800){
            thisACTIVITY()
        //}
        mAlertDialog.dismiss()
    }
    mAlertDialog.show()
}

private fun removeDEPT() {

    val dbHandler = DBHelper(this)

    val result = dbHandler.deleteDEPT(etDeptData.text.toString())

    if (result) {
        etDeptData.setText("")
        message("Record Removed")
        //Timer().schedule(1000){
            thisACTIVITY()
        //}
    }else{
        etDeptData.setText("NO MATCH -> click View Dept List")
        btnViewDeptList.visibility = View.VISIBLE
        btnEditDept.visibility = View.INVISIBLE
        btnDeleteDept.visibility =View.INVISIBLE
        btnSaveDeptData.visibility = View.INVISIBLE
        message("NO Match Found")
    }
}

OK all this is nothing without the XML file

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imgDI"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:src="@drawable/delete48" />

<TextView
    android:id="@+id/tvDAT"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="80dp"
    android:layout_marginTop="30dp"
    android:text="Delete Data"
    android:textColor="@color/color_Black"
    android:textSize="20sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tvDAC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="80dp"
    android:gravity="center"
    android:text="Click DELETE to Remove Data"
    android:textColor="@color/color_Black"
    android:textSize="18sp"
    android:textStyle="bold" />

<Button
    android:id="@+id/btnYES"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="240dp"
    android:layout_marginTop="110dp"
    android:background="@color/color_Transparent"
    android:text="DELETE"
    android:textColor="@color/color_deepBlue"
    android:textSize="18sp"
    android:textStyle="bold" />

<Button
    android:id="@+id/btnNO"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="110dp"
    android:background="@color/color_Transparent"
    android:text="CANCEL"
    android:textColor="@color/color_deepBlue"
    android:textSize="18sp"
    android:textStyle="bold" />

Vector
  • 3,066
  • 5
  • 27
  • 54