Title: acessing an EditText in an Custom View of an AlertDialog
Tags: xamarin;xamarin.android;android-custom-view;android-alertdialog
Question: How can i get the Text of an EditText, which is part of an layout for an Alert Dialog?
here the Code:
LayoutInflater li = LayoutInflater.From(this);
View view = li.Inflate(Resource.Layout.AlertDView1ET1TVhori, null);
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.SetTitle(GetString(Resource.String.EMPSPSAlertTitleMany));
adb.SetView(view);
EditText adbText = FindViewById<EditText&gt;(Resource.Id.txtADV11Left);
and the Layout:
<?xml version="1.0" encoding="UTF-8"?&gt;
<LinearLayout android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/widget28"&gt;
<EditText android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="test"
android:id="@+id/txtADV11Left"
android:layout_weight="5"
android:gravity="center"/&gt;
<TextView android:layout_height="wrap_content"
android:layout_width="0dp"
android:id="@+id/lbADV11Right"
android:text="Test"
android:layout_weight="1"
android:gravity="center"/&gt;
</LinearLayout&gt;
My problem is, that i don't even know how to access the EditText or the TextView in this layout. the last line of code won't find anything and at an later point i'll get an "System.NullReferenceException: Object reference not set to an instance of an object"
because adbText is null.
and please don't complain about my weird names for layouts an stuff.

Here is the accepted answer: You can't access it until it is created / linked to the activity.
Instead you can try this:
EditText adbText = view.FindViewById<EditText&gt;(Resource.Id.txtADV11Left);

Here is another answer: I've made a blog post about Dialogs on Mono for Android, which you might find interesting. As Softlion answered, one way is to find it from your inflated view:
var customView = LayoutInflater.Inflate (Resource.Layout.AlertDView1ET1TVhori, null);
var builder = new AlertDialog.Builder(this);
builder.SetView(customView);
builder.SetPositiveButton(Resource.String.dialog_ok, OkClicked);
builder.SetNegativeButton(Resource.String.dialog_cancel, CancelClicked);
//You should be able to access your EditText either here with:
var editText = customView.FindViewById<EditText&gt;(Resource.Id.txtADV11left);
You can also access the EditText in the EventHandlers like so:
private void OkClicked(object sender, DialogClickEventArgs args)
var dialog = (AlertDialog) sender;
var editText = (EditText)dialog.FindViewById(Resource.Id.txtADV11left);
Comment for this answer: and thanks to you too, i also will add your blog to my bookmarks, what i've seen there seemed to be really interesting

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:02/26/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<xamarin><xamarin.android><android-custom-view><android-alertdialog>', 'Title': 'acessing an EditText in an Custom View of an AlertDialog', 'ViewCount': 287, 'id': 15087060, 'user_AboutMe': '<p>Application developer with main knowledge in .NET development and mobile development with C#</p>\n', 'user_AccountId': 1896929.0, 'user_DisplayName': 'EaranMaleasi', 'user_Reputation': 865.0, 'user_Views': 103.0, 'user_id': 1713668.0}}