Android Webview - XML? - android

Is there a way to define a webview in the layout xml rather than in the code.
And if so how? Or is it recommended that it's coded in to an activity?

Yes as above use the WebView tag:
<WebView android:id="#+id/webview"
android:layout_width="fill_parent" android:layout_height="50dip"/>
A sample application can be found here:
http://www.androiddom.com/2011/04/creating-android-calculator-tutorial.html
The author creates a calculator that uses the WebView which is specified in the main.xml layout.

Yes, use the <WebView /> tag to do this in the xml layout.

and in your code type this :
WebView wv = (WebView)findViewById(R.id.webview);
wv.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv.loadUrl("your url here");

Related

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"?>

Android WebView- PopUp background is Transparent

I have loaded a WebView in my android app:
webView = (WebView)view.findViewById(R.id.webView);
String url = UrlConstants.URL_CITY;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
But every popUp that is of the website has a transparent background as shown:
I've tried many urls as well: One being this
Anyone help will be appreciated. Thanks
I had a similar issue when adding WebView at runtime using addView. Turns out you need to use the addView overload specifying LayoutParams with MATCH_PARENT. Failing to do so would break side menus and pop-up on some web pages. Really nasty bug if you ask me.

WebPage loading on browser instead of Webview in Activity [duplicate]

This question already has answers here:
Clicking URLs opens default browser
(6 answers)
Closed 8 years ago.
In my xml file I'm having text and below that text,I have placed a webview.
I am trying to open google's home page in Webview. Instead of opening in webview,webpage is opening on browser.What I want is web page should load in webview which is below some text.Below is my code:
<TextView
android:id="#+id/txt"
android:text="Hello Android"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#003399"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
/>
<WebView
android:id="#+id/webview"
android:layout_marginTop="50dp"
android:layout_below="#id/txt"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
Kindly help to solve this.Thank you
This is because a combination of two things:
the WebViewClient is not set (set to null), this makes the WebView try and offer every navigation as an intent to the system. Since you have a browser installed the system will try to handle that navigation there.
going to google.com ususally results in a redirect, which is why the stuff about navigations in the previous point matters.
Try this:
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient());
Create your activity and than use this code...
mWebview = (WebView) findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
Toast.makeText(youractivity_name.this, description, Toast.LENGTH_SHORT).show();
}
});
mWebview.loadUrl("http://www.yahoo.com");

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

Add javascript into WebView

I am developing an application where in 1 part I want to add javascript into WebView.. But am not getting how to do it in an appropriate way.. Can anyone pls guide me into this?????
I am doing it like:
wb=(WebView)findViewById(R.id.webView1);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setPluginState(WebSettings.PluginState.ON);
wb.getSettings().setPluginsEnabled(true);
wb.loadUrl("javascript:<script " ></script> ");
wb.setWebViewClient(new HelloWebViewClient());
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
It was very simple..also without using any javascriptInterface..In my code, Instead writing:
wb.loadUrl("javascript:<script> </script>");
use,
wb.loadDataWithBaseURL(null,"<script> </script>","text/html","utf-8",null);
and its working now :)
Look at these nice tutorials about how to implements javascript in webview in android..
I think its provides you all the information what you needed..
Android WebView
Android WebView, Javascript and CSS
EDIT: Further if your implemented code having any exception or not working then please post that code and exception then here we can help you..

Categories

Resources