I am trying to play an youtube video inside an android app by using the url of the video.I have tried using the following code:
new Thread(new Runnable() {
#Override
public void run() {
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient());
try {
myWebView.loadUrl(icobj.video_URL);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
It works, but the problem is that there is a black screen shown for about 4 seconds before the page opens.How do i get rid of the black screen before the page opens?
Check this link. You can listen the webview and show a placeholder or progress bar until loaded. It is all up to you.
Related
I'm making an Android App with Java in Android Studio for my website using webview, but i've a lot of links which go out of website. How can i detect if link redirect from www.mywebsite.com to www.anotherwebsite.com and open it in another page intent?
You can use the code below.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
and create and inner class
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
You can find complete documentation here
I have a webview loading a big texts after the user has clicked an object.
I like the text to be shown from the top. But if the user scrolled down in the previous text the next is already scrolled and not shown from the top.
I am using webView.scrollTo(0,0); in my onPageFinished event of the webview.
This works fine in my emulator, but on my phone the text is scrolled in the middle.
when adding a pause with Thread.sleep it also works on my phone.
What am I doing wrong here ?
My Code:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// Pause is needed for my physical phone S7. In Emulator there I have no problem.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
webView.scrollTo(0,0);
super.onPageFinished(view, url);
}
});
Try with this block of code -
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// Pause is needed for my physical phone S7. In Emulator there I have no problem.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
view.scrollTo(0,0);
super.onPageFinished(view, url);
}
});
Actually, you're so close to the solution. Just the point is you need to set scrollTo in the returned view of onPageFinished method.
I am loading a SharePoint page into a webview using LoadUrl. All the other content comes down but not the images. The site requires a cookie to login but that part is working fine its just once you get past the login page the next page is correct minus the images.
String myUrl = "https://www.mysite.com/";
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(myUrl, cook);
CookieSyncManager.getInstance().sync();
webView = (WebView)findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient()
{
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
{
handler.proceed();
}
public void onProgressChanged(WebView view, int progress)
{
}
});
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true); webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
webView.loadUrl(myUrl);
You might have the same problem as I do.
Is it possible that the picture source require authentication? You can try to open the sources separately and see if you can access them or not.
I did the same and found out that the image sources I wanted to show are protected via a cookie and right now I'm looking for a solution to send this cookie along but couldn't find a way yet.
When I read you question that is exactly the same you try to do.
( sorry to write this here but I don't have enough points to write a comment )
I have a webview and a link on the URL that I have loaded on that webview, I want such like that if user clicks in this link the app will go to the background and open that link with the default web browser of that device. To do this I have done the following:
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
web.getSettings().setUseWideViewPort(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.getSettings().setPluginState(PluginState.ON);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null
&& url.startsWith("http://the6figuremarketer.com/apps/androidfb21/thks.html")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
web.loadUrl("http://the6figuremarketer.com/apps/androidfb21/index.html");
but I figured out that the shouldoverrideurl method is nevet get called. what should I do now? can anyone help?
Look at shouldOverrideUrlLoading in WebView for Android not running.
shouldOverrideUrlLoading() is not called when you call loadurl on webview. It is called only when you click on a link in webview.
What I'm trying to do:
I want the user to be able to custom gestures and name them "up","down"..etc. Then when the user runs my application, it loads up a WebView with a particular url like http://google.com. Then the user can draw the gesture he created earlier. So if he draws the "up" gesture, then another webview opens.
My problem:
When I try to draw the gesture over the webview, it doesn't draw. Rather it scrolls the webview.
What have I tried:
Already created a GestureOverlayView and put the WebView within that. Unfortunately, the GestureOverlayView is unable to detect the gestures as the WebView gets scrolled.
My Code:
public class WebViewCenter extends Activity implements OnGesturePerformedListener {
/** Called when the activity is first created. */
GestureLibrary mLibrary;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//WebView webview = (WebView)findViewById(R.id.WebView01);
//webview.loadUrl("http://www.google.com");
//Button btnStart = (Button)findViewById(R.id.Start);
//btnStart.setOnClickListener(this);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures_overlay);
gestures.addOnGesturePerformedListener(this);
WebView webview = (WebView)findViewById(R.id.WebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://live.com");
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
mLibrary.load();
ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
Log.i("Default Marker", predictions.get(0).toString());
if (predictions.size() > 0) {
String action = predictions.get(0).name;
Log.i("Gesture", action);
if ("up".equals(action)) {
Log.i("Up_gesture", "Up gesture detected");
Intent int_up = new Intent(WebViewCenter.this, WebViewUp.class);
WebViewCenter.this.startActivity(int_up);
//return true;
//Toast.makeText(this, "Adding a contact", Toast.LENGTH_SHORT).show();
}
/*else
{
Log.i("Marker 2", "god knows what ur upto");
}*/
}
}
Any advice as to how can I separate the WebView scrolling from the gesture building?
Thanks,
Pawan