PDF in WebView opens browser automatically - android

Hi everyone i want to display a pdf in my activity using a webview so i did:
XML
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.lasergroup.ami.utilities.PDFActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wv_PDF"/>
</RelativeLayout>
JAVA
WebView wv = (WebView)findViewById(R.id.wv_PDF);
String pdfURL = "http://www.randomurl.com/randompdf.pdf"
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginState(WebSettings.PluginState.ON);
wv.loadUrl("http://docs.google.com/gview?embedded=true&url="+pdfURL);
This worked well on emulator but when i use it on my device (LG G3 Android 5.0 API 21) it opens the browser (Chrome) automatically after wv.loadUrl. This is a very strange issue, so i want to understand if it's a code problem or a device one, and i want to know if someone can tell me why this happens.
I haven't any intent to open the browser so i can't understand what happens (only on some devices).

You should add WebViewClient before call loadUrl
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});

Related

Android youtube site in Webview is jamming

I try to load youtube website using WebView in Android application. Need to load whole youtube site to WebView (not only one certain video url). It means user select video on youtube site and then play. But the playing of video is jammming. Is it wrong way using youtube inside application ?
I am using this code: webView.loadUrl(url);
pls Follow this code it will work for you
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView mWebview ;
private String youtubeUrl = "https://www.youtube.com/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebview = (WebView) findViewById(R.id.youtubeWebView);
mWebview.loadUrl(youtubeUrl);
mWebview.getSettings().setJavaScriptEnabled(true);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/youtubeWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.constraint.ConstraintLayout>
and add Internet Permission inside your Manifest file
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
it will looks like this in the below pic.
you can load any url into web view and add the internet permission inside your app's manifest file and use like below code --
yourWebView.loadUrl(youtubeUrl);
this works for me , i am sure it will work mate

opening a html file in a web-view in android studio

I added every thing to my app but it keeps opening the page that said page not found even if i add a URL for google or any other link.
I look at most of the solutions for the same question but none of them worked (allowing internet access, allowing local access, changing the link to android asset, ......)
Any way hers my code hope you can help:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url="http://www.google.com";
WebView home = (WebView) this.findViewById(R.id.homeweb);
home.loadUrl(url);
home.setWebChromeClient(new WebChromeClient());
home.clearCache(true);
home.getSettings().setAppCacheEnabled(false);
}
}
And here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.alex.myapplication.MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/homeweb" />
</RelativeLayout>
I know it's very simple in code but I'm still new to programming HTML in Android.
Just follow below code for showing html code in browser,
WebView view = new WebView(getActivity());
view.setVerticalScrollBarEnabled(false);
((LinearLayout)rootView.findViewById(R.id.text_about)).addView(view);
view.loadData(Html.fromHtml(put your url here), "text/html; charset=utf-8", "utf-8");
put html file in assets folder then write following code in activity
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/your_file_name.html");
remove these line from xml code
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
also remove code from activity
home.setWebChromeClient(new WebChromeClient());
home.clearCache(true);
home.getSettings().setAppCacheEnabled(false);
Programs run from top to bottom.So your problem is just the arrangement of your code.
In the onCreate method, use this code:
setContentView(R.layout.activity_main);
webView = (webView) findViewById(R.id.webView_id);
webSettings websettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//After configuring all the required settings then you load your url
webView.loadurl("https://www.google.com");
webView.setWebViewClient(new webViewClient());

How to open a webpage in the app not in the browser via App Inventor

I want to build an app in App Inventor, I am unable to do it in Eclipse. I want to build a webpage in the app. How do I open a webpage in the app, and not in the browser?
I have tried this:
ActivityStarter1
Action:android.intent.action.VIEW
DataUri:http://example.com
But it's opening in the browser only.
start a new activity with WebView in activity_web_view.xml
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setBackgroundColor(0);
webView.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Toast.makeText(getApplicationContext(),"loading...", Toast.LENGTH_LONG).show();
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getApplicationContext(),"loaded", Toast.LENGTH_LONG).show();
}
});
webView.getSettings().setPluginState(PluginState.ON);
webView.loadUrl("http://stackoverflow.com");
use a Webviewer component like this
A very good way to learn App Inventor is to read the free Inventor's Manual here in the AI2 free online eBook http://www.appinventor.org/book2 ... the links are at the bottom of the Web page. The book 'teaches' users how to program with AI2 blocks.
There is a free programming course here http://www.appinventor.org/content/CourseInABox/Intro and the aia files for the projects in the book are here: http://www.appinventor.org/bookFiles
How to do a lot of basic things with App Inventor are described here: http://www.appinventor.org/content/howDoYou/eventHandling .
Also do the tutorials http://appinventor.mit.edu/explore/ai2/tutorials.html to learn the basics of App Inventor, then try something and follow the Top 5 Tips: How to learn App Inventor
You will not find a tutorial, which does exactly what you are looking for. But doing the tutorials (not only reading a little bit) help you to understand, how things are working. This is important and this is the first step to do.
You need to use WebView.
Add it to your activity like this
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
and then provide url like this (for instance in the onCreate of the Activity):
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");
you also need to provide the internet access permission in the manifest file
<uses-permission android:name="android.permission.INTERNET" />
For details look at
http://developer.android.com/guide/webapps/webview.html
first of all you need to make that web page on default browser by using html code or any programing app or web page maker, then convert that page into apk file.
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
If you want to deliver a web application (or) just a web page as a part of a client application, you can do it using WebView. The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page. Then convert that page through app studio
</xml version="2.0" encoding="utf=-8"?>
<webview xmlns:android="http://example.com/apk/res/android/version"
android:id="#+id/webview"
android:layout_witdth_height="fill parent"
<manifest ... >
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
<,xml version="3.0" encoding="utf-9"?>

Open text link in web view

I have the text in textview that is "Open the link http://www.google.com to find anything." The text i have set in my class file. SO, when i click on this link. The link is open in browser. I want open this link in my web view. How can i do this?
My xml file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="444dp" />
Do something like this,
in your oncreate method,
WebView mywebView = (WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new MyWebViewClient());
And then create a class like,
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("somePartOfYourUniqueUrl")){
return super.shouldOverrideUrlLoading(view, url);
} else {
view.loadUrl(url); // load url in webview
return true;
}
}
Use java.net.URL for this
Here is a link for your help, using this you may parse url from given string
How to detect the presence of URL in a string
You can do that, with WebView, you need to declare a WebViewClient object and override the public boolean shouldOverrideUrlLoading (WebView view, String url) method, there you can filter urls or give some customized functionality.
In your case, to stay on the WebView, you would need to return false on that method.
Check out this tutorial.
To handle the click event on the TextView's url. As it's suggested on this question you can filter the ACTION_VIEW intent on your WebView containing Activity. If you need more guidance about intent-filters, check this out.
For a TextView its a little bit more work, You'll have to make your own copy of the Linkyfy class and use a TransformFilter to make the links behave however you want them to. Check out for an example Android Linkify class both web and #mentions all in the same TextView

Android webview twitter link not opening

The link below is not opening in android webview .I am new to android. Someone help me to open this link in webview.
https://mobile.twitter.com/SCHOOLinSITES
I tried to open it in the following manner
XML
<WebView android:id="#+id/webView51"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
Activity
WebView w=(WebView)findViewById(R.id.webView51);
w.getSettings().setDomStorageEnabled(true);
w.getSettings().setJavaScriptEnabled(true);
w.loadUrl("https://mobile.twitter.com/SCHOOLinSITES");
Set a userAgent in your Webview.
webView.getSettings().setUserAgentString("Android");
webView.loadUrl("https://mobile.twitter.com/SCHOOLinSITES");

Categories

Resources