Working with custom layout of an alertdialog

1

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

But even with the activity, I can not get their ID's, obviously, because they are not in the activity layout.

So how can I work with this layout designed to do what needs to 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:

Edit

The answers made me leave the 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()
}
    
asked by anonymous 22.12.2017 / 13:29

2 answers

1

Do this:

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();
    
22.12.2017 / 13:40
1

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

The value passed to the first parameter of the onClick method of DialogInterface.OnClickListener, in this case identified as p0 , is a DialogInterface.

Through it you can access the views using

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

I recommend changing the names of parameters p0 and p1 to dialog and which , to better understand what they represent.

    
22.12.2017 / 15:31