How to get EditText value from another layout?

Good day all programmers

Currently I'm developing an android app using Visual Studio 2015. What the app does is, when the button is clicked, then AlertDialog will pop up. This alert dialog has a EditText that the user can enter something, and then will Toast it. The problem that I encounter is, whatever the user enter, will not be shown in the Toast.

My Main.axml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px"> <Button android:text="Show Input Dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/show" /> </LinearLayout>

My inputDialog.axml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="50px"> <TextView android:text="Enter Something Here" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/Title" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/userInput" android:hint="HERE!!!!!" /> </LinearLayout>

My MainActivity.cs

`Button show;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        //INITIALIZE
        show = FindViewById<Button>(Resource.Id.show);

        //EVENT
        show.Click += delegate { clickShow(); };
    }

    private void clickShow()
    {
        LayoutInflater inflate = (LayoutInflater)GetSystemService(LayoutInflaterService);
        View view = inflate.Inflate(Resource.Layout.inputDialog, null);

        RunOnUiThread(() =>
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.SetView(view);

            builder.SetNegativeButton("Cancel", delegate { builder.Dispose(); });
            builder.SetPositiveButton("Change", delegate { clickChange(); });
            builder.Show();
        });
    }

    private void clickChange()
    {
        try
        {
            EditText userInput = FindViewById<EditText>(Resource.Id.userInput);

            Toast.MakeText(this, userInput.Text, ToastLength.Long).Show();
        }
        catch
        {
            Toast.MakeText(this, "No Input", ToastLength.Long).Show();
        }
    }`

GIF showing I try it on real device (Lenovo A1000, Android Version 5.0)

image

I've tried to search all over google, but can't find what I need. Sorry if this is a silly question.

Thank you for your time guys. Appreciate it. Thanks again.

Best Answer

  • ChristopheBERNARDChristopheBERNARD BE ✭✭✭✭
    Accepted Answer

    Hello @MuhammadAziziAbdulAziz

    in my application when i run the popup with the runUIThread i do this

    `

    this.RunOnUiThread(() =>
    {

                    //
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    alert.SetTitle(Resource.String.txtDoYouWantInputParcelManually);
    
                    //add edit text on popup
                    EditText input = new EditText(this);
                    input.InputType = Android.Text.InputTypes.TextFlagNoSuggestions;
                    input.SetFilters(new Android.Text.IInputFilter[] { new Android.Text.InputFilterLengthFilter(maxLength) });
    
                    //add input
                    alert.SetView(input);
    
                    //Ok button
                    alert.SetPositiveButton(Resource.String.positiveButtonManually, (senderAlert, arg) =>
                    {
            //GET VALUE FROM INPUT
                        _inputManuel = input.Text;
                    });
    
                    ........
    

    `

    _inputManuel the variable for to receive the value enter ...

    hope this help

Answers

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭
    Accepted Answer

    Hello @MuhammadAziziAbdulAziz

    in my application when i run the popup with the runUIThread i do this

    `

    this.RunOnUiThread(() =>
    {

                    //
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    alert.SetTitle(Resource.String.txtDoYouWantInputParcelManually);
    
                    //add edit text on popup
                    EditText input = new EditText(this);
                    input.InputType = Android.Text.InputTypes.TextFlagNoSuggestions;
                    input.SetFilters(new Android.Text.IInputFilter[] { new Android.Text.InputFilterLengthFilter(maxLength) });
    
                    //add input
                    alert.SetView(input);
    
                    //Ok button
                    alert.SetPositiveButton(Resource.String.positiveButtonManually, (senderAlert, arg) =>
                    {
            //GET VALUE FROM INPUT
                        _inputManuel = input.Text;
                    });
    
                    ........
    

    `

    _inputManuel the variable for to receive the value enter ...

    hope this help

  • Hello Muhammad,

    You could try following this link about Passing Data when Navigating in Xamarin (forms). Although this link is for Xamarin.Forms, it might give you an idea for a solution in Xamarin.Android.

    Michael

  • AziziAzizAziziAziz USMember ✭✭

    @ChristopheBERNARD Thank you so much for your answer and your time. It works perfect! You just saved my day. Thank you.

    Here's a GIF showing the successful result. Thanks!

    image

  • AziziAzizAziziAziz USMember ✭✭

    @MichaelKalisvaart

    Sir, thank you for your answer and your time. However, @ChristopheBERNARD answer is perfect for me. Thank you again.

    Have a nice day guys. Bless you both. :smiley:

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    Hello @MuhammadAziziAbdulAziz

    thanks you so much for the return that's great to help you and that my solution works for you !
    sorry for my english :)

    i'm a beginner too with mobile and ask often some question here, and when i have some free time i tried to help some guy ...

    it programming is great for this :)

    have a nice coding day

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    you video make me smile this morning :) thanks

Sign In or Register to comment.