How to read the value from the username text box of my login page loaded in the xamarin android web

I'm trying to get the value in the user name text box from my login page after the user entered it in a web page using webview in Xamarin android.

I tried using "document.getelementbyid('useranme').values"

but it is always returning null values. Can any one please help ?

Best Answer

  • EswaraprabuEswaraprabu ✭✭
    Accepted Answer

    Eventually I have found the solution for my problem.

    I have user OnLoadResource function in webview which helped me to get the datas I was searching for

        public override void OnLoadResource(WebView view, string url)
        {
            try
            {
                view.EvaluateJavascript("document.getElementById('username').value;", this);
    
            }
            catch (Java.Lang.Exception oe)
            {
    
            }
            base.OnLoadResource(view, url);
        }
    

    Thanks.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Please note the js string is case sensitive here. You need to modify it to:

    EvaluateJavaScriptAsync($"document.getElementById('name').value;");
    

    I created a simple demo here. You can click the button to retrieve the text in the input.

  • EswaraprabuEswaraprabu Member ✭✭

    Hello Landlu,

    Thank-you So much for you time.

    Actually I working on Native Xamarin Android. Please find the implementation below.

    protected override void OnCreate(Bundle bundle)
    {

            var webView = FindViewById<WebView>(Resource.Id.webView);
            var myURL = "myurl";
    
            webView.LoadUrl(myURL);  
            webView.SetWebViewClient(new EmbeddedWebViewClient(webView));
            webView.Settings.JavaScriptEnabled = true;
            webView.Settings.BuiltInZoomControls = true;
            webView.Settings.SetSupportZoom(true);
            webView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
            webView.ScrollbarFadingEnabled = false;
    

    }

    I have used EmbeddedWebViewClient for loading the web-view.

    public class EmbeddedWebViewClient : WebViewClient, IValueCallback
    {
        public EmbeddedWebViewClient(WebView view)
        {
            view.Settings.JavaScriptEnabled = true;
            view.Settings.DomStorageEnabled = true;
        }
    
        public override void OnPageFinished(WebView view, string url)
        {
            base.OnPageFinished(view, url);
    
            view.EvaluateJavascript($"document.getElementById('usernameinput').value;", this);
        }
    
        public void OnReceiveValue(Java.Lang.Object value)
        {
            Log.Info("Value -------------------->", value.ToString());
        }
    }
    

    this is code i have referred. whenever I pass this javascript it is not returning the text in the textbox of the webview.

    Can you kindly help me on this.

  • LandLuLandLu Member, Xamarin Team Xamurai

    You called the js method when webview finished loading. It won't get the correct text as the input hasn't interacted.
    Try to add a button in your layout and get the text there:

    See the attachment for the specific code on Android.

  • EswaraprabuEswaraprabu Member ✭✭

    Thank you for you help. I'm getting to under more information about my problem !!

    The problem I'm facing here is the button is also in the webview

    The requirement is to load the webview and to collect the username details to find which user has logged in.

    I'm not having the control over the webpage. So I can only load it and read the user entered values in the user name text box. I can't add additional button in the screen.

    I want to get the username within the override methods available in the webview.

    Can you kindly help on this ?

  • LandLuLandLu Member, Xamarin Team Xamurai

    @Eswaraprabu So your question is how to call the native code on the webpage.
    Create a class for JavascriptInterface:

    public class JavaScriptInterface : Java.Lang.Object
    {
        [Export]
        [JavascriptInterface]
        public void GetUserName(string userName)
        {
            // Retrieve the name here
        }
    }
    

    Add it to your webview:

    webView.AddJavascriptInterface(new JavaScriptInterface(), "android");
    

    At last, trigger from the webpage:

    function sendUserName() {
        var inputVal = document.getElementById("useranme").value;
    
        if (window.android) {
            window.android.GetUserName(inputVal);
        }
    }
    
  • EswaraprabuEswaraprabu Member ✭✭
    Accepted Answer

    Eventually I have found the solution for my problem.

    I have user OnLoadResource function in webview which helped me to get the datas I was searching for

        public override void OnLoadResource(WebView view, string url)
        {
            try
            {
                view.EvaluateJavascript("document.getElementById('username').value;", this);
    
            }
            catch (Java.Lang.Exception oe)
            {
    
            }
            base.OnLoadResource(view, url);
        }
    

    Thanks.

Sign In or Register to comment.