title: Cookie in Webview android
body: <p>I am struggling with using cookie along with javascript in webview android.</p>
  
  <p>I have one HTML page which is located in sdcard data. This html file consist of javasript and cookie. I need to load this html file in webview and webview needs to respond to javascript as well as cookie.</p>
  
  <p>Work done so far:-</p>
  
  <p>Enabled javascript for webview</p>
  
  <pre><code>WebView1.getSettings().setJavaScriptEnabled(true);
  </code></pre>
  
  <p>Also tried to enable cookie using CookieManager</p>
  
  <pre><code>CookieManager.getInstance().setAcceptCookie(true);
  </code></pre>
  
  <p>Doing this, webview respond to javascript but not cookies. Any Idea how to solve this problem?</p>
  
  <p>HTML file is as follows</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;script&gt;
              function setCookie(cname, cvalue, exdays) {
                      var d = new Date();
                      d.setTime(d.getTime() + (exdays*24*60*60*1000));
                      var expires = "expires="+d.toUTCString();
                      document.cookie = cname + "=" + cvalue + "; " + expires;
              }
  
              function getCookie(cname) {
                  var res = "No Cookie!";
                      var name = cname + "=";
                      var ca = document.cookie.split(';');
                      for(var i=0; i&lt;ca.length; i++) {
                          var c = ca[i];
                          while (c.charAt(0)==' ') c = c.substring(1);
                          if (c.indexOf(name) != -1) res = c.substring(name.length, c.length);
                      }
                  document.getElementById('txt').innerHTML = res;
              }
          &lt;/script&gt;&lt;noscript&gt;No JavaScript!&lt;/noscript&gt;
      &lt;/head&gt;
  
      &lt;body onload="setCookie('id', 'Cookie is working properly!', '1');"&gt;        
          &lt;p id="txt"&gt; &lt;/p&gt;
          &lt;center&gt;
              &lt;a href="./test-2.html"&gt;Next Page&lt;/a&gt;
          &lt;/center&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When i open this File in browser, its working fine. If someone wants to open this file in browser then please use the following url <a href="https://www.ypsid.com/demo/test/test-1.html" rel="nofollow">https://www.ypsid.com/demo/test/test-1.html</a></p>