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>
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()
}