Handle font-face With jsoup
Hello sir you can handle your webview with jsoup
First of all you need implementation jsoup
implementation 'org.jsoup:jsoup:1.11.1'
then you need fetch your site like this
Document doc = Jsoup.connect("http://youramazing.site").get();
after that you can add your custom style to your document with append method
doc.head().append(" <style>\n" +
" @font-face {\n" +
" font-family: 'Roboto Slab Bold';\n" +
" src: url('file:///android_asset/fonts/syne.ttf')\n" +
" }\n" +
" body { font-family: 'Roboto Slab Bold', serif; font-size: 17px; color: #000; }\n" +
" a { color: #000; }\n" +
" </style>");
after that you can handle it with webview
String s = doc.html() webView.loadData(s,"text/html","utf-8");
Full code with AsyncTask:
public class MainActivity extends AppCompatActivity {
WebView webView;
Document doc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.wewewew);
AsyncTask<Void,Void,String> asyncTask = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
try {
doc = Jsoup.connect("http://youramazing.site/").get();
} catch (IOException e) {
e.printStackTrace();
}
doc.head().append(" <style>\n" +
" @font-face {\n" +
" font-family: 'Roboto Slab Bold';\n" +
" src: url('file:///android_asset/fonts/syne.ttf')\n" +
" }\n" +
" body { font-family: 'Roboto Slab Bold', serif; font-size: 17px; color: #000; }\n" +
" a { color: #000; }\n" +
" </style>");
return doc.html();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
webView.setWebViewClient(new WebViewClient());
webView.loadData(s,"text/html","utf-8");
}
};
asyncTask.execute();
}
}