23 jun 2017

CODE: Mostrar una web como nativa en una app andoid .apk


PASO 1
\app\src\main\AndroidManifest.xml

<!-- INI AGREGADO -->
<!-- Otorgamos permisos de Internet a nuestra App -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- FIN AGREGADO -->
PASO 2
\app\src\main\res\layout\activity_main.xml
<!-- INI AGREGADO -->
<!-- Creamos una actividad webview para mostrar nuestra pagina web -->
<WebView
 android:id="@+id/activity_main_webview"
 android:layout_height="match_parent"
 android:layout_width="match_parent" />
<!-- FIN AGREGADO -->
PASO 2.1
Entrar en modo Design y borrar el padding
PASO 3
\app\MainActivity.java
// INI AGREGADO
private WebView mWebView;
// FIN AGREGADO
PASO 3.1
// INI AGREGADO
 mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Activamos javascript
 WebSettings webSettings = mWebView.getSettings();
 webSettings.setjavascriptEnabled(true);
// Url que carga la app (webview)
 mWebView.loadUrl("https://mundopoetico.es/");
// Forzamos el webview para que abra los enlaces internos dentro de la la APP
 mWebView.setWebViewClient(new WebViewClient());
// Forzamos el webview para que abra los enlaces externos en el navegador
 mWebView.setWebViewClient(new MyAppWebViewClient());
// FIN AGREGADO
PASO 3.2
// INI AGREGADO
 @Override
// Detectar cuando se presiona el botón de retroceso
 public void onBackPressed() {
 if(mWebView.canGoBack()) {
 mWebView.goBack();
 } else {
 super.onBackPressed();
 }
 }
// FIN AGREGADO
PASO 4
\app\MyAppWebViewClient.java
Creamos nueva clase con el nombre "MyAppWebViewClient” y OK.Borramos todo el codigo de la clase y ponemos el siguiente:
package com.demo.demo.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) {
// Url base de la APP (al salir de esta url, abre el navegador) poner como se muestra, sin https://
 if(Uri.parse(url).getHost().endsWith("google.com")) {
 return false;
 }
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
 view.getContext().startActivity(intent);
 return true;
 }
}
// FIN AGREGADO

PASO 5 (Opcional)
\app\src\main\res\values\styles.xml
<!-- INI AGREGADO -->
<!-- Ocultamos la barra de titulo -->
 <item name="android:windowNoTitle">true</item>
<!-- FIN AGREGADO -->



Fuente: http://tutorialesenlinea.es/439-mostrar-pagina-web_dentro-de-una-aplicacion-android-apk.html

Etiquetas:

Prueba anterior