How can we get info from a webpage(link on it that downloads an excel file when you click on it)? What subjects should be searched in order to do that on Android? Thanks.
AFAIK, you will not able to know what are the link is for. There is a way to know about the link of the webview on which user clicks and it is WebViewClient.
Here is a code using which you can know that the clicked link has XLS file or pdf file or others. You can also block the link using that code
public class TestingActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView view = new WebView(getApplicationContext());
view.setWebViewClient(new MwebViewClient());
}
class MwebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(".xls")) {
Log.d("Blah", "This link contains XLS file");
}
return super.shouldOverrideUrlLoading(view, url);
}
}
}
Related
I am using webView in my application and have another activity with different posts, I want to attach my webView to those posts so that if I click on post 1 the webPage which is linked to it opens up and if I click on post 2 the webPage linked to that post opens up in same webView, is there any way to do so. Also I am using firebase as a database and fetching my web URL from the database. Please help.
I am using Android Studio
webView class file:
public class webViewNews extends AppCompatActivity {
private WebView webviewthis;
private DatabaseReference mdataRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_page);
mdataRef = FirebaseDatabase.getInstance().getReference().child("webing");
webviewthis = (WebView) findViewById(R.id.webView_news);
webviewthis.setWebViewClient(new WebViewClient());
webviewthis.getSettings().setJavaScriptEnabled(true);
webviewthis.getSettings().setLoadsImagesAutomatically(true);
mdataRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
post2web webView = dataSnapshot.getValue(post2web.class);
webviewthis.loadUrl(webView.getWebViewPost());
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
Fetch the url of that post and pass it with intent to webview activity.
Instead of using activity for webview, it is better to open webview in fragment.
It would be faster and easier
try this create one method like this
public static void callWebview(Context context, String url) {
Intent i = new Intent(context, webViewNews.class);
i.putExtra("url", url);
context.startActivity(i);
}
when you need to start webview activity than just call this methos like this
callWebview(this,"https://stackoverflow.com");
I am trying to get current html page path from "asset" folder in cordova android app, when we changing the pages inside the cordova webview.
I am using only MainActivity
public class MainActivity extends CordovaActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadUrl(launchUrl);
}}
I searched and tried my level best to find the solution. But still now I cannot get the right solution. I am totally confused, Please suggest any solution for the followings,
Any function available to detect cordova webview URL when page changed
like public boolean shouldOverrideUrlLoading(WebView view, String url) {
} method?
Can we create WebViewClient for Cordova Webview?
You can get the current url with
appView.getUrl();
To get url changes you can use this:
SystemWebView wv = (SystemWebView)appView.getView();
wv.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine)appView.getEngine()){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// you can get the current url with view.getUrl()
// or the url that it's trying to load next with url
return false;
}
});
I want to read pdf files and display contents on TextView. is it possible ? or just show pdf into WebView or pdfViewer?
i want to do like it,
public class MainActivity extends Activity {
private TextView showText;
String url="http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showText= (TextView)this.findViewById(R.id.showtext);
showText.setText( ++ convertPdfTotext ++);
}
public void convertPdfTotext(View view)
{
}
}
If you want to use it in text view you can convert it with itext as string,but you'll loose formatting:
PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("C:/Text.pdf"));
parser.getTextFromPage(3);
Use WebView instead and you can enable even zoom.
WebView mWebView=new WebView(MyPdfViewActivity.this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+LinkTo);
I have a native android app in which there are two activities, 1st activity contains a button and on action of button I want to start the 2nd activity. Content of seconds activity will be from a website. Is it possible to make the 2nd activity behave like webapp ?
If I use WebView in this case, it is displaying the website inside the activity but when I try to interact with the website, it ask to open it in the browser which I don't want. I want to interact with the website within the activity.
Any help will be highly appreciated.
Thanks a ton.
Use below code... for open link in WebView itself instead of browser...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
WebView engine = (WebView) findViewById(R.id.web_view);
engine.loadUrl("https://play.google.com/store?hl=en");
engine.setWebViewClient( new HelloWebViewClient() );
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading( WebView view, String url ) {
return false;
}
}
see this link for more details... WebView should not open links in browser
I think if you set the baseURL property when creating the Webview, you'll get what you want.
Create you're own Webview where you can override the default behaviour:
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
android webview click opens default browser
I have an application that opens a website in webview on start, however I can't seem to figure out how to keep any URL's that a user clicked on from opening in the browser.
Is there anyway to load the clicked URL inside the webview app?
EDIT - Using the below solution I still get an error on the private class "Illegal modifier for the local class MainScreenActivity; only abstract or final is permitted"... even if I try to rename this I get the same error
public class MainScreenActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.talk-of-the-tyne.co.uk");
setContentView(webview);
private class MainScreenActivity extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
}
Apologies if I am being thick about this, I just can't seem to get my head around this one
try this code:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());