Change Text of Texview of Custom AlertDialog?

lukasrein97lukasrein97 USMember ✭✭✭

Hy guys,

I have a Custom AlertDialog with a Custom View.
I want to get the TextViews of the Custom AlertDialog and Update them with a text when the AlertDialog pop up.

This is what i have atm but it doesnt work.

Any Solutions?

                            TextView firma = FindViewById<TextView>(GuscheinQR.Resource.Id.Firma);
                            firma.Text = companyresult;
                            TextView guthaben = FindViewById<TextView>(GuscheinQR.Resource.Id.guthaben);
                            guthaben.Text = "€ " + guthabenresult;

Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this, GuscheinQR.Resource.Style.MyAlertDialogStyle);
                            alert.SetTitle("Akzeptiert");
                            alert.SetView(GuscheinQR.Resource.Layout.AlertDialogAkzeptiert);
                            alert.SetPositiveButton(" Abbuchen", (senderAlert, args) =>
                            { });
                            alert.SetNegativeButton("Abbrechen ", (senderAlert, args) =>{
                            });
                            Dialog dialog = alert.Create();
                            dialog.Show();

Best Answer

Answers

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    The reason it does not work is, that you set the text to a view that might not be exist in the layout you pass to the SetView method.

    In my example you can see how to add an EditText to your AlertView. But you can easily exchange it with your TextView.

                    var _categoryName = new EditText(this);
                    _categoryName.Hint = "Kategoriename";
    
                    var _dialogBuilder = new AlertDialog.Builder(this);
                    _dialogBuilder.SetMessage("Bitte geben Sie einen Kategorienamen ein")
                        .SetTitle("Kategorie anlegen")
                        .SetPositiveButton("Speichern", AddCategory)
                        .SetNegativeButton("Abbrechen", CancelDialog)
                        .SetView(_categoryName)
                        .Show();
    
  • lukasrein97lukasrein97 USMember ✭✭✭

    @SebastianSeidel.9226 said:
    The reason it does not work is, that you set the text to a view that might not be exist in the layout you pass to the SetView method.

    In my example you can see how to add an EditText to your AlertView. But you can easily exchange it with your TextView.

                    var _categoryName = new EditText(this);
                    _categoryName.Hint = "Kategoriename";
    
                    var _dialogBuilder = new AlertDialog.Builder(this);
                    _dialogBuilder.SetMessage("Bitte geben Sie einen Kategorienamen ein")
                        .SetTitle("Kategorie anlegen")
                        .SetPositiveButton("Speichern", AddCategory)
                        .SetNegativeButton("Abbrechen", CancelDialog)
                        .SetView(_categoryName)
                        .Show();
    

    Yeah thanks for that but i needed to Inflate my layout because i had a custom layout, not only a textview. So like in the solution i postet a few mins befor i needed to inflate my customlayout with layoutinflater, then set the view(inflated layout) to the adapter and finally after the view gets called in my alertdialog, search for the textboxes and set the text i want to them.

Sign In or Register to comment.