Raditya Kurnianto
Raditya Kurnianto

Reputation: 744

Display text when clicked on the Button of AlertDialog in android

I'm working with android and I want to make an event on AlertDialog button. I want to change the text on the button dynamically, this is my code

AlertDialog.Builder alert = new AlertDialog.Builder(Soto_Betawi.this);
            alert.setTitle("Payment");
            alert.setMessage("Total Price : Rp. " + total);
            final EditText input = new EditText(Soto_Betawi.this);
            alert.setView(input);
            alert.setPositiveButton("Change Due", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which)
                {
                    cash = Integer.parseInt(input.getText().toString());
                    change = cash - total;
                    //I want to set a text from the operation above in a text
                }
            });
            alert.setNegativeButton("Close", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which)
                {

                }
            });
            alert.setCancelable(true);
            alert.create().show();

Upvotes: 1

Views: 189

Answers (3)

Seshu Vinay
Seshu Vinay

Reputation: 13588

Another Solution for you:

(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE)

This Should give you positive button of the AlertDialog

So my assumption is

(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText("Your Text");

should be your answer.

Upvotes: 0

Krishna Prasad
Krishna Prasad

Reputation: 691

Create and show alertdialog from method:

public void showAlert(String txtPositiveButton) {
    AlertDialog.Builder alert = new AlertDialog.Builder(Soto_Betawi.this);
    alert.setTitle("Payment");
    alert.setMessage("Total Price : Rp. " + total);
    final EditText input = new EditText(Soto_Betawi.this);
    alert.setView(input);
    alert.setPositiveButton(txtPositiveButton, new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {
            cash = Integer.parseInt(input.getText().toString());
            change = cash - total;
            //I want to set a text from the operation above in a text
        }
    });
    alert.setNegativeButton("Close", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {
        }
    });
    alert.setCancelable(true);
    alert.create().show();
}

Upvotes: -1

Seshu Vinay
Seshu Vinay

Reputation: 13588

Instead of using AlertDialog, you can use Activity with Dialog theme and create your own layout. So that you can simply change the text with

myButton.setText("your text");

Upvotes: 2

Related Questions