Working with custom alertdialog layout

Asked

Viewed 528 times

1

I created this layout, so I could work with this alertdialog. I created it, because I have to record the states after the click and also, when opening the dialog, I can get the value that was marked earlier. Search through the preferences.

But even bringing Activity, I can’t get your ID’s, obviously, because they’re not in the Activity layout.

So how can I work with this layout created to do what must be done?

Code:

R.layout.alertdialog_radiobutton_account >> my layout

Function:

fun onWhoCanContactMeClicked(activity : Activity) {

val dialog: AlertDialog.Builder = AlertDialog.Builder(activity)
dialog.setTitle(activity.getString(R.string.whocancontact_settings))
        .setView(R.layout.alertdialog_radiobutton_account)
        .setPositiveButton(activity.getString(R.string.ok_dialog)) { p0, p1 ->
            //TODO After click set state
        }
        .setNegativeButton(activity.getString(R.string.cancel_dialog)) { p0, p1 ->
            p0.dismiss() //dismiss dialog
        }
        .create()
        .show()
}

XML:

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

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>

        <variable
            name="viewModel"
            type="com.plugapps.zuk.viewmodel.AccountViewModelKotlin">

        </variable>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/margin_large"
        android:orientation="vertical">

        <RadioGroup
            android:id="@+id/rdg_dialog_account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/margin_large"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/rdb1_contact"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:text="@string/everyone_dialog"
                android:textSize="16sp" />

            <RadioButton
                android:id="@+id/rdb2_contact"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:text="@string/onlymycontacts_whocancontact_settings"
                android:textSize="16sp" />

            <RadioButton
                android:id="@+id/rdb3_contact"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:text="@string/my_contacts"
                android:textSize="16sp" />

        </RadioGroup>
    </LinearLayout>
</layout>

Layout image: inserir a descrição da imagem aqui

Edit

The answers got me out of place, I’ll leave exactly as I built the code here:

fun onWhoCanContactMeClicked(activity: Activity) {

    val li: LayoutInflater = activity.layoutInflater
    val view: View = li.inflate(R.layout.alertdialog_radiobutton_account, null)

    var rgb1: RadioGroup = view.findViewById(R.id.rdg_dialog_account)
    var rdb0: RadioButton = view.findViewById(R.id.rdb1_contact)
    var rdb1: RadioButton = view.findViewById(R.id.rdb2_contact)
    var rdb2: RadioButton = view.findViewById(R.id.rdb3_contact)

    when (whoCanContactMe) { //esta variável é um Int
        1 -> rgb1.check(rdb1.id)
        2 -> rgb1.check(rdb2.id)
        else -> rgb1.check(rdb0.id)
    }

    val dialog: AlertDialog.Builder = AlertDialog.Builder(activity)
    dialog.setTitle(activity.getString(R.string.whocancontact_settings))
            .setView(view)
            .setPositiveButton(activity.getString(R.string.ok_dialog)) { dialog, wich ->
                whoCanContactMe = when {
                    rdb1.isChecked -> 1
                    rdb2.isChecked -> 2
                    else -> 0
                }
            }
            .setNegativeButton(activity.getString(R.string.cancel_dialog)) { p0, p1 ->
                p0.dismiss() //dismiss dialog
            }
            .create()
            .show()
}
  • Related: https://answall.com/q/4682/2541

2 answers

1


Do so:

final LayoutInflater li = LayoutInflater.from(MainActivity.this);

 View view = li.inflate(R.layout.layout_dialog_numero_trasferir, null);

 final EditText editText = (EditText)view.findViewById(R.id.edt_dlg_numero);

 view.findViewById(R.id.btn_dlg_ok).setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {


                        numero = Integer.parseInt(editText.getText().toString());


                        MainActivity.alerta.dismiss();

                }
            });

            view.findViewById(R.id.btn_dlg_cancel).setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    MainActivity.alerta.dismiss();
                }
            });

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle(MainActivity.this.getString(R.string.lbl_titulo_transferencia));
            builder.setView(view);
            alerta = builder.create();
            alerta.show();

1

I suppose you want to get references to the views of the dialog layout when one of the buttons is clicked.

Value passed to first method parameter onClick Onclicklistener, in this case identified as p0, is a Dialoginterface.

Through it is possible to access the views using

((Dialog) p0).findViewById(R.id.nomeDaView);

I advise you to change the parameter names p0 and p1 for dialog and which, for a better understanding of what they represent.

  • Thank you! the variables are the ones that Kotlin generates. You say that, I must have a case(when) to see which has the check when clicking the positive button? Am I correct? And, my question turns, also, around tb, the layout be inflated with a predetermined value, coming from sharedpreferences, for example

  • "You say, I must have a case(when) to see which has the check when clicking the positive button?" - No, in the answer I do not mean this. Regarding the second part, I had not understood that I intended to pre-fill the dialog views,

  • I made an Edit in the post. I asked because, I have 3 radiobuttons, and I needed to set the value of what is selected.

  • Yes, it was only after seeing the issue that I understood what I intended.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.