Open text link in web view - android

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

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.

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

How to display Web site in Android WebView

I am new to Android WebView, in my application i need to display a web site conatins 3 web pages. In the first web page there will be a link to navigate to the second page and second to third page. I given the URL in the WebView, the first page is displayed perfectly, when i click the link it directly opens the browser application to display the second page. But i want to display the second page in the WebView itself. Please find my code below:
WebView forumView=(WebView)findViewById(R.id.forumView);
forumView.getSettings().setJavaScriptEnabled(true);
forumView.loadUrl("url");
As i said i am very new to WebView my code might be wrong, please help me to solve this problem.
Thanks in Advance,
Rajapandian
This piece of code will help you.
wbb = (WebView) findViewById(R.id.webView_tobe_loaded);
WebSettings wbset=wbb.getSettings();
wbset.setJavaScriptEnabled(true);
wbb.setWebViewClient(new MyWebViewClient());
String url="http://www.google.com";
System.out.println(getdeviceid());
wbb.getSettings().setJavaScriptEnabled(true);
wbb.loadUrl(url);
You'll have to intercept the clicks yourself if you don't want the default Android behavior.
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

Android: Callbacks with WebView component?

I am doing authentication with a third-party site that's supposed to redirect back to my app with auth token (OAUTH).
I have the callback working properly if I open the 3rd party site in a separate browser process via
this.startActivity(new Intent(Intent.ACTION_VIEW, uri));
but, if I embed a WebView component in my layout, and open the url in that, the callback does not work. Webview says "You do not have permission to open myapp://callback?token=...." and quickly refreshes to "Web page not available...temporarily down...blah blah"
Any ideas?
You need to implement a WebViewClient to intercept the custom URI before it is loaded. The Hello, WebView tutorial shows a simple example. Where they have:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
You can change "view.loadUrl(url)" to check if the URL is your custom URL and handle it however you want.

Categories

Resources