En este tutoriales en linea les mostraremos los siguientes fragmentos de código Java que podemos utilizar en Android Studio.Android WebView inicia el navegador al llamar a loadurl
Si desea entregar una aplicación web (o solo una página web) como parte de una aplicación cliente, puede hacerlo utilizando WebView. La clase WebView es una extensión de la clase View de Android que le permite mostrar páginas web como parte de su diseño de actividad. No incluye ninguna característica de un navegador web completamente desarrollado, como controles de navegación o una barra de direcciones. Todo lo que hace WebView, por defecto, es mostrar una página web.
MyAppWebViewClient.java
package com.example.alfa.tutoriales(Esto lo cambia por el nombre tu tu App);
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
// INI AGREGADO
public class MyAppWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("tel:")){
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
// FIN AGREGADO
Android WebView inicia el navegador al llamar a loadurl
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dedline);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://google.com");
}
WebViewActivity.onCreate(...)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
final PullScrollView refreshLayout = (PullScrollView) findViewById(R.id.refresh_layout);
final WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setjаvascriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
//设置头部加载颜色 - Establecer el color de carga de la cabeza
refreshLayout.setHeaderViewColor(R.color.colorAccent, R.color.dark ,android.R.color.white);
refreshLayout.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
refreshLayout.setRefreshListener(new PullScrollView.RefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
webView.loadUrl("http://www.stay4it.com");
refreshLayout.setRefreshCompleted();
}
}, 1000);
}
});
refreshLayout.refreshWithPull();
}
}
¿Cómo cargar un url para webview en Android?
WebSettings settings = webview.getSettings();
settings.setjаvascriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
webview.loadUrl("http://www.google.com");
Android: carga de la salida de WebView en el Widget de la aplicación
webView = new WebView(this);
webView.setVerticalScrollBarEnabled(false);
webView.setWebViewClient(client);
winManager.addView(frame, params);
webView.loadUrl("http://stackoverflow.com");
private final WebViewClient client = new WebViewClient() {
public void onPageFinished(WebView view, String url) {
final Point p = new Point();
Autorización Oauth 2.0 para LinkedIn en Android
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl(authorizationUrl);
Log.i("Authorize","Loading Auth Url: "+authUrl);
webView.loadUrl(authUrl);
¿Cómo puedo obtener el evento onclick en webview en Android?
webView.setOnTouchListener(this);
client = new WebViewClient(){
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
handler.sendEmptyMessage(CLICK_ON_URL);
webView.setWebViewClient(client);
webView.setVerticalScrollBarEnabled(false);
webView.loadUrl("http://www.example.com");
¿Es posible acceder a los datos del formulario HTML POSTADOS a través de un WebView?
setContentView(R.layout.browser);
WebView browser = (WebView)findViewById(R.id.browser_window);
browser.getSettings().setjаvascriptEnabled(true);
browser.addjаvascriptInterface(new jаvascriptInterface(), "HTMLOUT");
browser.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
inyectar CSS a un sitio con webview en Android
webView.getSettings().setjаvascriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
webView.loadUrl("https://www.google.com");
inputStream.close();
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
webView.loadUrl("jаvascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var style = document.createElement('style');" +
¿Cómo configurar diferentes títulos para el cuadro de diálogo de alerta cuando se carga la página WebView?
super.onCreate(savedInstanceState);
WebView web=new WebView(this);
web.setWebViewClient(new WebViewClient(){
web.loadUrl("http://www.google.com");
builder=new AlertDialog.Builder(this);
builder.setView(web);
Android WebView no carga la URL
public class WebViewDemo extends Activity {
private WebView webView;
Activity activity ;
private ProgressDialog progDailog;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview_compontent);
webView.getSettings().setjаvascriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
});
webView.loadUrl("https://tutorialesenlinea.es");
}
WebActivity.initWebView()
private void initWebView() {
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onprogressChanged(WebView view, int newProgress) {
super.onprogressChanged(view, newProgress);
if (newProgress == 100) {
swipeRefreshLayout.setRefreshing(false);
if (contentLayout != null)
contentLayout.showContent();
if (webView != null)
url = webView.getUrl();
} else {
if (contentLayout != null)
contentLayout.showLoading();
}
}
});
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setjаvascriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl(url);
}
DictionaryFragment.onViewCreated(...)
def = (TextView) view.findViewById(R.id.tv_def);
wikiWebView = (WebView) view.findViewById(R.id.wv_wiki);
wikiWebView.getSettings().setLoadsImagesAutomatically(true);
wikiWebView.setWebViewClient(new WebViewClient());
wikiWebView.getSettings().setjаvascriptEnabled(true);
wikiWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
¿Cómo obtener contenido html de una vista web?
setContentView(R.layout.webview);
final WebView webview = (WebView) findViewById(R.id.browser);
webview.getSettings().setjаvascriptEnabled(true);
webview.addjаvascriptInterface(new MyjаvascriptInterface(this), "HtmlViewer");
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webview.loadUrl("jаvascript:window.HtmlViewer.showHTML" +
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
webview.loadUrl("http://android-in-action.com/index.php?post/" +
"Common-errors-and-bugs-and-how-to-solve-avoid-them");
webChromeClient abre el enlace en el navegador
WebView web = (WebView)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setjаvascriptEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setSupportMultipleWindows(true); // This forces ChromeClient enabled.
web.setWebChromeClient(new WebChromeClient(){
@Override
public void onReceivedTitle(WebView view, String title) {
getWindow().setTitle(title); //Set Activity tile to page title.
}
});
web.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Que puede reemplazar la función capturePicture
webView.loadUrl("http://developer.android.com/reference/packages.html");
webView.setWebViewClient(new WebViewClient() {
¿Cómo escuchar un WebView terminando de cargar una URL?
private void setupWebViewClient() {
webView.setWebViewClient(new WebViewClient() {
private int running = 0; // Could be public if you want a timer to check.
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String urlNewString) {
running++;
webView.loadUrl(urlNewString);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
running = Math.max(running, 1); // First request move it to 1.
}
@Override
public void onPageFinished(WebView view, String url) {
if(--running == 0) { // just "running--;" if you add a timer.
// TODO: finished... if you want to fire a method.
}
}
});
}
Abre la URL en WebView en lugar del navegador predeterminado
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.getSettings().setjаvascriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String url = getURL();
if (mWebView != null && url != null) {
mWebView.loadUrl(url);
¿Cómo puedo realizar una captura de pantalla de una vista web mediante programación, capturando la página completa?
super.onCreate(savedInstanceState);
w = new WebView(this);
w.setWebViewClient(new WebViewClient()
w.loadUrl("http://google.com/search?p=android");
Muestra una fórmula matemática atractiva en Android
setContentView(R.layout.main);
final WebView w = (WebView) findViewById(R.id.webview);
w.getSettings().setjаvascriptEnabled(true);
w.getSettings().setBuiltInZoomControls(true);
w.loadDataWithBaseURL("http://bar", "<script type='text/x-mathjax-config'>"
+ "MathJax.Hub.Config({ "
+ "src='file:///android_asset/MathJax/MathJax.js'"
+ "></script><span id='math'></span>", "text/html", "utf-8", "");
w.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!url.startsWith("http://bar")) return;
w.loadUrl("jаvascript:document.getElementById('math').innerHTML='\\["
+ doubleEscapeTeX("sample string") + "\\]';");
w.loadUrl("jаvascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);");
Alerta no aparece desde la vista web en Android?
setContentView(R.layout.main);
WebView webview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setjаvascriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webview.requestFocusFromTouch();
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/test.html");
Comentarios