Yo tenía un código donde podía mostrar un Splash antes de que se cargase el webview de mi página en la app. Sin embargo, no podía loguearme con facebook debido a sus pop-ups. A pesar de esto, encontré un código salvador que hizo que funcionase, aunque tuve que sacar una clase llamada MyAppWebViewClient para colocar el UriWebViewClient y ahora no sé cómo ni dónde colocar el Splash. ¿Alguien puede ayudarme? Estos son los archivos:
main.activity.java:
public class MainActivity extends Activity {
/* URL saved to be loaded after fb login */
private static final String target_url="http://www.chapatelo.com.ar/";
private static final String target_url_prefix="www.chapatelo.com.ar";
private Context mContext;
private WebView mWebview;
private WebView mWebviewPop;
private FrameLayout mContainer;
private long mLastBackPressTime = 0;
private Toast mToast;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// final View controlsView =
// findViewById(R.id.fullscreen_content_controls);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
mWebview = (WebView) findViewById(R.id.activity_main_webview);
//mWebviewPop = (WebView) findViewById(R.id.webviewPop);
mContainer = (FrameLayout) findViewById(R.id.webview_relative);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
mWebview.setWebViewClient(new UriWebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.splashLoading1).setVisibility(View.GONE);
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}
});
mWebview.setWebChromeClient(new UriChromeClient());
mWebview.loadUrl(target_url);
mContext=this.getApplicationContext();
}
private class UriWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
//Log.d("shouldOverrideUrlLoading", url);
if (host.equals(target_url_prefix))
{
// This is my web site, so do not override; let my WebView load
// the page
if(mWebviewPop!=null){
mWebviewPop.setVisibility(View.GONE);
mContainer.removeView(mWebviewPop);
mWebviewPop=null;
}
return false;
}
if(host.equals("m.facebook.com") || host.equals("www.facebook.com")){
return false;
}
// Otherwise, the link is not for a page on my site, so launch
// another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
Log.d("onReceivedSslError", "onReceivedSslError");
//super.onReceivedSslError(view, handler, error);
}
}
class UriChromeClient extends WebChromeClient {
@Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
mWebviewPop = new WebView(mContext);
mWebviewPop.setVerticalScrollBarEnabled(false);
mWebviewPop.setHorizontalScrollBarEnabled(false);
mWebviewPop.setWebViewClient(new UriWebViewClient());
mWebviewPop.getSettings().setJavaScriptEnabled(true);
mWebviewPop.getSettings().setSavePassword(false);
mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mContainer.addView(mWebviewPop);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(mWebviewPop);
resultMsg.sendToTarget();
return true;
}
@Override
public void onCloseWindow(WebView window) {
Log.d("onCloseWindow", "called");
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.chapatelo.www.chapatelo_beta.MainActivity"
android:id="@+id/webview_relative">
<WebView
android:layout_width="match_parent"
android:layout_height="641dp"
android:id="@+id/activity_main_webview"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="@+id/splashLoading1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:visibility="visible"
android:src="@mipmap/splash"
android:contentDescription="@string/desc"/>
</FrameLayout>
Nota: La clase anterior que tenía para ejecutar el Splash antes de que se cargue el webview era la siguiente (MyAppWebViewClient.java):
public class MyAppWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(Uri.parse(url).getHost().endsWith("chapatelo.com.ar")){
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
Y así manejaba el Splash (ImageView):
mWebView.setWebViewClient(new MyAppWebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.splashLoading1).setVisibility(View.GONE);
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}
});
¿Alguien podría indicarme cómo puedo hacer que la ImageView aparezca desde que se abra la app hasta que se cargue el WebView y que desaparezca tal y como hacía en el último código que mostré?