In activity.xml i am using this code
<android.support.v4.widget.NestedScrollView
android:id="#+id/web_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/wv_brow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.80">
</WebView>
</android.support.v4.widget.NestedScrollView>
In Activity i am using this code
brow= (WebView)findViewById(R.id.wv_brow);
nestedScrollView = (NestedScrollView) findViewById(R.id.web_scroll_view);
nestedScrollView.scrollTo(0,0);
brow.setWebViewClient(new OurViewClient());
brow.loadUrl( webURL );
this is working fine it is open website . Issues this that if i scroll Web Page and click any URL then next web page is not open from top.Please help me how can i open next page from top
You can assign a WebViewClient and do this to scroll it top every time a new page opens:
brow.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView webView, String url) {
webView.scrollTo(0,0);
}
});
As you already have your OurViewClient, I would suggest overriding the onPageFinished method and put the scrollTo method inside.
Related
I have one WebView which has a RelativeLayout parent.
in WebView when i click on some specific button, it loads another url. i want to disable keyboard on that url. and that url i got in shouldoverrideurlloading(WebView view , Url Url) method.
<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:background="#drawable/background"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context="com.xenopsi.benchmark.BrowserActivity">
<WebView
android:id="#+id/browserView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:scrollbars="none"/>
</RelativeLayout>
On that method i did:
relativeLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
webView.setFocusable(false);
webView.setFocusableInTouchMode(true);
but still when i tap on text field of that page , keyboard still shown.
if i did same code on onCreate() method of activity, its work fine,
but my problem is want to disable it on shouldOverrideUrlLoading method when my page load.
Okey as you mention you want to hide keyboard for particular link, do something like this,
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("Loading URL", url);
view.loadUrl(url);
if(url.equals(yourUrl)){
// hide keyboard
} else {
// unhide keyboard
}
return true;
}
I hope this will Help..
Happy Coding..
I need to create a floorplan which can be scrolled and zoomed.Some sections of this plan shall be linked to other activities (ie. kitchen). My idea was to lay a simple button on the kitchen-region. With a click on it you can get to the kitchen activity.
Because of scrolling- and zooming-requirement I created a WebView containing my floorplan:
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".FloorplanActivity"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="#+id/webViewFloorplan"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<Button
android:id="#+id/buttonKuehltheke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</WebView>
</LinearLayout>
But now I'm not able to lay a button on a specific region on the floorplan. The Button always appears in upper left corner of my floorplan.
MainActivity-Code:
WebView wvFloorplan = (WebView) findViewById(R.id.webViewFloorplan);
wvFloorplan.getSettings().setBuiltInZoomControls(true);
wvFloorplan.loadUrl("file:///android_asset/services_plan.png");
What needs to be done to set the button to every postition on my WebView (Floorplan)?
Thx Arne
If you are going to be scrolling and zooming this WebView, the coordinates for the kitchen section are going to be dynamic, so positioning a button over it will be difficult.
I would suggest overriding WebView.shouldOverrideUrlLoading. This way you can use html to create a link to the kitchen activity.
wvFloorplan.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("act:")) {
Intent intent = new Intent( <kitchen activity> );
startActivity(intent);
return true;
}
return false;
}
});
You will have to write an html wrapper around the image to map parts of your images to different links but now the coordinates will be static. Instead of http urls use Href="act:kitchen"
My webview (initially invisible) gets url to load:
#Override
public void onCreate(Bundle savedInstanceState){
...
webView.loadUrl("some url");
...
}
Then, in onClickListener I change its visibility to visible and see nothing loaded. If I set visibility to visible and THEN load url, it works ok, but user should wait for some time while url is loading. I want to preload everything and show ALREADY LOADED webpage. How to do it? Webview loads something if only it is visible...
P.S. btw, I tried making the webview visible and setting translationY large enough the view to go below screen - still, it loads nothing unless I invoke setTranslationY(0); ... what to do?
P.P.S. I tried invoking webView.loadData - same effect.
try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_web);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.co.uk");
mWebView.setWebViewClient(new HelloWebViewClient());
}
public void testing (View view){
mWebView.setVisibility(View.VISIBLE);
}
Button in screen_web.xml:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="testing"
android:text="Button" />
WebView in screen_web.xml:
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="0.32"
android:visibility="invisible" />
It works well for me.
How do disable and hide the address bar from a WebView?
There is no address bar in a WebView.
If you think you have a WebView, and you see an address bar, that is not your WebView. Rather, you are looking at the Browser application. Most likely, the URL you told the WebView to load did a redirect, and you did not intercept that redirect using a WebViewClient and shouldOverrideURLLoading().
Adding myView.setWebViewClient(new WebViewClient()); disabled the address bar for me.
import android.webkit.WebView;
import android.webkit.WebViewClient;
...
WebView myView = findViewById(R.id.myExampleView);
myView.setWebViewClient(new WebViewClient());
myView.getSettings().setJavaScriptEnabled(true);
myView.loadUrl("https://www.stackoverflow.com");
XML Snippet
<WebView android:id="#+id/myExampleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:gravity="center" />
source: (Japanese site):
http://www.techdoctranslator.com/android/webapps/webview
Finally I Try with this. Its worked for me..
Here is the working code
private WebView webview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ebook);
//webview use to call own site
webview =(WebView)findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient());
webview .getSettings().setJavaScriptEnabled(true);
webview .getSettings().setDomStorageEnabled(true);
webview.loadUrl("http://www.google.com");
}
and your entire main.xml(res/layout) look should 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"
/>
don't go to add layouts.
webview.setWebViewClient(new WebViewClient());
solved the problem for me..
Kotlin code as following
myWebView.setWebViewClient(WebViewClient())
I've been banging my head against a wall this afternoon trying to get a WebView to work. Below is the code in the main class:
public class fkyougoogle extends Activity {
/** Called when the activity is first created. */
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
// WORKS
//webview.loadUrl("http://www.google.com");
// DOESN'T WORK
//webview.loadUrl("http://www.theregister.co.uk");
//webview.loadData("<html><body>hello</body></html>", "text/html", "utf-8");
//webview.loadDataWithBaseURL("fake://", "<html><body>hello</body></html>", "text/html", "utf-8", "http://www.theregister.co.uk/");
}
}
This is Google's "Hello, Webview" example. If I use a WebView and try to access www.google.com then it works fine. If I try to access any other site then it fails including loadData and it just displays a black screen in the emulator. In the end I would like to read from a local file.
is included under the manifest tag and the XML schema is the same as the Hello Webview example.
Am I missing something obvious here? :(
Try changing
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to
android:layout_width="fill_parent"
android:layout_height="fill_parent"
in your main.xml top level LinearLayout
It should look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Try UTF-8 instead of utf-8 for your latter two attempts. I have no problem loading http://www.theregister.co.uk using the same code -- try loading it in the built-in Browser app, and if that fails, you're perhaps encountering some sort of firewall/proxy issue.
Here are a few projects demonstrating simple uses of WebView, from one of my books.
I had similar problem of a WebView being completely blank, but it my case it was caused by a missing android.permission.INTERNET uses-permission.