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

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");

Related

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());

Android webview can not load web pages

I can not load web pages using webview,Nothing is displayed,
this url "http://dev.51yunche.com:7000/WeChat/Service%20introduce.html",
this is my webview
webView = (WebView) findViewById(R.id.drive_web);
webView.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDomStorageEnabled(true);//DOM Storage
webView.loadUrl("http://dev.51yunche.com:7000/WeChat/Service%20introduce.html");
this xml:
<WebView
android:id="#+id/drive_web"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
The console does not have any prompts or error messages。I'd like to hear some input from more experienced WebView users (and developers?).
Thank you in advance!
This is not webview Issue.In web browser also that url display Nothing.check that Backend or design code of your html file.
You have a space encoded character %20 in your URL...
use a valid url like:
webView.loadUrl("http://dev.51yunche.com:7000/WeChat/Serviceintroduce.html");
Alternatively you can also try URLEncoder.encode(YOUR_URL); function to encode the Url
Your link itself doesn't display anything in web browser. If you still have any doubt, just simply create a normal html page by yourself and run it from your local system and check it out.

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

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");

Categories

Resources