android - Add CSS to WebView -


note i'm reading html file res>raw>index.html(folder hierarchy) has style.css file.

but how add css file... i'm not using "file:///android_asset" path

here code :

package com.veereshc.veer.vturesults;  import android.content.context; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.keyevent; import android.view.menu; import android.view.menuitem; import android.webkit.webchromeclient; import android.webkit.webview; import android.webkit.webviewclient; import android.widget.toast;  import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.inputstream;   public class mainactivity extends actionbaractivity {  webview webview; context context; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     webview = new webview(this);     setcontentview(webview);     context = this;     webviewclient client = new webviewclient();     webview.setwebviewclient(client);            try {            inputstream stream = this.getassets().open("index.html");             int streamsize = stream.available();             byte[] buffer = new byte[streamsize];             stream.read(buffer);             stream.close();             string html = new string(buffer);             webview.loaddata(readtextfromresource(r.raw.index), "text/html", "utf-8");      }     catch (ioexception e){         e.printstacktrace();     }    }  private string readtextfromresource(int resourceid) {     inputstream raw = getresources().openrawresource(resourceid);     bytearrayoutputstream stream = new bytearrayoutputstream();     int i;     try     {         = raw.read();         while (i != -1)         {             stream.write(i);             = raw.read();         }         raw.close();     }     catch (ioexception e)     {         e.printstacktrace();     }     return stream.tostring(); } @override public void onbackpressed() {     // todo auto-generated method stub     if (webview.cangoback()) {         webview.goback();         return;     } else {         super.onbackpressed();     } }   @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }     @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); } 

}

you can add inline styles index.html file:

<style>     body {         background-color: green;     }      h1 {         color: red;         margin-left: 40px;     }     ... </style> 

or can create styles.css, include them:

<link rel="stylesheet" href="styles.css"> 

and use webview#loaddatawithbaseurl:

webview.loaddatawithbaseurl("file:///android_res/raw/", loaddata(readtextfromresource(r.raw.index), "text/html", "utf-8", ""); 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -