Criei este layout, para poder trabalhar com este alertdialog. O criei, pois tenho que gravar os estados após o clique e também, ao abrir o dialog, eu conseguir pegar o valor que foi marcado anteriormente. O buscar através do preferences.
Mas, mesmo trazendo a activity, não consigo buscar seus ID's, obviamente, pelo motivo deles não estarem no layout da activity.
Então, como posso trabalhar com este layout criado para fazer o que deve ser feito?
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
As respostas me fizeram sair do lugar, vou deixar exatamente como construi o código aqui:
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()
}