I want to get the link of an image clicked inside a Webview to display it in an new intent. The site that i want the image from is not opening the images in fullscreen when clicked.
I want something like the code down bellow but the website doesn't open images in the same tab.
Reference Code:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url == null){
return false;
}else if (url.trim().toLowerCase().endsWith(".img")) {//use whatever image formats you are looking for here.
String imageUrl = url;//here is your image url, do what you want with it
}else{
view.loadUrl(url);
}
}
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url == null){
return false;
}else if (url.trim().toLowerCase().endsWith(".img")) {//use whatever
image formats you are looking for here.
String imageUrl = url;//here is your image url, do what you want with
it
Intent intent = new Intent(this, NewActivity.class);
//pass from here the data to next activity and load there
intent.putExtra("yourURL", imageUrl);
startActivity(intent)
}else{
view.loadUrl(url);
}
}
}
Related
I want to stop loading the url in same webview when clicking any item within the webpage.
for this I have tried shouldoverrideurlloading() but its not catching the url so not able to override it.
our application is single page application that may be reason due to which shouldOverrideUrlLoading() is not getting called.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return handleUri(view,uri);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return handleUri(view, uri);
}
private boolean handleUri(WebView view,final Uri uri){
if (uri.toString().contains("messages") || uri.toString().contains("proddetail") || uri.toString().contains("about-us")) {
Intent intent = new Intent(webActivity.this, nativeActivity.class);
startActivity(intent);
return true;
}
return true;
}
In this its not catching url containing messages..
Another method I have tried is catching url using doUpdateVisitedHistory() and using view.stoploading() for stopping the loading of url within same webview.
#Override
public void doUpdateVisitedHistory(WebView view, String url,boolean isReload) {
if ( url.contains("messages"))
{
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
view.stopLoading();
//opening new fragment
SharedFunctions.getInstance().replaceFragment(CompanyPage.this, new CompanyPage(uri.toString().replace("www","m")), "Messages", getActivity().getSupportFragmentManager(), true, true);
}
}, 1000);
}
Using this the loading of url is not stopping as when coming back to previous fragment that loading has been done in webview.
I'm trying to create a application that uses webview in order to load a page. In this page i have some link that i need to open them with another intent with an application called MX Player for video playing. But the problem is that my webview even if i click in a link that doesn't ends with mp4 or a link that ends with mp4 it does nothing. Here is my code :
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.getSettings().setAllowFileAccess(true);
webView.loadUrl("LINK OF MY PAGE");
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
url = webView.getUrl();
if (url.endsWith(".m3u8") || url.endsWith(".ts") || url.endsWith(".amv") || url.endsWith(".mp4"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri videoUri = Uri.parse(url);
intent.setDataAndType( videoUri, "application/x-mpegURL" );
intent.setPackage( "com.mxtech.videoplayer.ad" );
startActivity( intent );
}
webView.loadUrl(url);
return true;
}
});
}
}
According to the documentation shouldOverrideUrlLoading (WebView view, String url) should return
true if the host application wants to leave the current
WebView and handle the url itself, otherwise return false.
This means that your overriden shouldOverrideUrlLoading should return true if you want to handle the URL by yourself, and false if you don't want to handle the URL, and WebView should open it. So your code should look like this:
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
//url = webView.getUrl(); --remove this. You already have the URL in String url parameter
if (url.endsWith(".m3u8") || url.endsWith(".ts") || url.endsWith(".amv") || url.endsWith(".mp4"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri videoUri = Uri.parse(url);
intent.setDataAndType( videoUri, "application/x-mpegURL" );
intent.setPackage( "com.mxtech.videoplayer.ad" );
startActivity( intent );
return true; // to tell the WebView that it should not load the URL because you handled it by yourself
}
//webView.loadUrl(url); remove this. You need to return true or false to tell the WebView if it should load the url, or not
return false; //to tell WebView that you didn't handle the URL, and it should load it.
}
This question already has an answer here:
Android WebView - Intercept clicks
(1 answer)
Closed 6 years ago.
How to view toast when i clicked an url in webview without go to url, only display a toast message?
wv = (WebView) findViewById(R.id.wV);
wv.loadData("[0]", "text/html", "UTF-8");
if(OnClickedUrl == "#_ftn0")
Toast.makeText(getApplicationContext(), OnClickedUrl, Toast.LENGTH_LONG).show();
you should override the url loading like below:
wv.loadData("[0]", "text/html", "UTF-8");
wv.setWebViewClient(new MyWebViewClient());
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Toast.makeText(getApplicationContext(), url, Toast.LENGTH_LONG).show();
if(url.equal(urlyouclicked)){//here you compare the url you click
Toast.makeText(getApplicationContext(), OnClickedUrl, Toast.LENGTH_LONG).show();
}
return true;
}
}
You can create a shouldOverrideUrlLoading1 event (refer to link 1 for API reference) and place your code within the method. However you will need to specify the url(s) where the loading should be overridden. If you want to override for all urls just remove the if statement.
wv.setWebViewClient( new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//Make sure the url to override loading is the correct one
if (url.contains("www.xxx.com")) {
return true;
}
return false;
}
#Override
public void onLoadResource(WebView view, String url){
//Make sure the url to override loading is the correct one
if (url.contains("www.xxx.com")) {
//create toast message here
}
}
});
Besides override shouldOverrideUrlLoading the url should match this format:"://",or it will not work. So you can modify "#_ftn0" to "example://#_ftn0"
This question already has answers here:
WebView link click open default browser
(6 answers)
Closed 9 years ago.
I have a webview that loads in a fragment in Android. When the user taps a link on the webpage loaded in the webview, I want the new page to be loaded in the default browser on Android outside of my app. Any ideas how to do this? Thanks!
What I have so far:
FrameLayout layout = new FrameLayout(context);
WebView webView = new WebView(getActivity());
webView.setBackgroundColor(Color.TRANSPARENT);
webView.loadUrl(InvestBetterURL);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return false;
}
#Override
public void onPageStarted (WebView view, String url, Bitmap favicon)
{
view.setVisibility(View.GONE);
}
#Override
public void onPageFinished (WebView view, String url)
{
view.setVisibility(View.VISIBLE);
}
});
layout.addView(webView);
Change the shoudlOverrideUrlLoading to true. Check the link:
http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
UPDATED:
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("google.com")) {
view.loadUrl(url);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
return true;
}
}
This is a part of my android app, i have created a webview...but when i click on any link inside the webview...nothing happens....here is my code...i want to launch the link either in any browser or any installed download manager...i m a newbie..please help me out with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the Up button in the action bar.
setupActionBar();
// no need to use title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// set webview as main content only
mWeb = new WebView(this);
setContentView(mWeb);
// set Javascript
WebSettings settings = mWeb.getSettings();
settings.setJavaScriptEnabled(true);
// the init state of progress dialog
mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
// add a WebViewClient for WebView, which actually handles loading data from web
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// when finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
// set url for webview to load
mWeb.loadUrl("http://vesit-d7b.host22.com/Assignments/ECCF.html");
}
Please use this code and check that your url is being caught by the webview or not.
myweb.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
Toast.makeText(this,url,Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
see what url is coming when you click the link.....
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.v("here","the link is ::" + url);
view.loadUrl(url);
return true;
}
and then copy it on browser to see whether it is a valid link or not..
#Ritesh remove the shouldOverrideUrlLoading method it will work