Android: How to disable keyboard on webview for some specific page? - android

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..

Related

How to open url from top in webview

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.

Android: how to put webview + 1 more view in scrollview?

I need to scroll webview. Thats simple. But when webview is scrolled till end, I want one more view to appear as if webview and that one more view were in single scroll view. How to do that properly?
Consider that its incorrect to put webview inside scrollview.
P.S. In other words, it should look like there is a webview with footer. The footer is regular android view.
A roughly method can be the following: place your UIWebView inside a UIScrollView and set the consentSize according to your needs (ie. at the end of the UIWebView contentSize append another UIView of your choice):
#import "ViewController.h"
#interface ViewController () {
UIScrollView *aScrollView;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
aScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
[self.view addSubview:aScrollView];
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
aWebView.delegate = self;
[aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com"]]];
[aScrollView addSubview:aWebView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webview
{
CGFloat yourWebViewContentHeight = [[webview stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight"] floatValue];
[aScrollView setContentSize: CGSizeMake(aScrollView.frame.size.width, yourWebViewContentHeight + 80)];
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, yourWebViewContentHeight, self.view.frame.size.width, 80)];
aView.backgroundColor = [UIColor redColor];
[aScrollView addSubview:aView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
For more fine grain you have to deal with scrollviewdelegate but this can be a good startpoint.
Just Add Webview in Scroll View it will automatically Disable scroll of WebView, See This Demo
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/svc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/white">
<WebView
android:id="#+id/wv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>

Android: Button on an WebView-Element (.png-Image)

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"

Android WebView focus problem

I am fighting with focus management of WebView: WebView messes with focus management of classic components. Here is a simple application with just an EditBox and a WebView in it which loads Google!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:and="http://schemas.android.com/apk/res/android"
xmlns:webtag="http://schemas.android.com/apk/res/com.webtag"
and:orientation="vertical" and:layout_width="fill_parent" and:layout_height="fill_parent">
<LinearLayout and:id="#+id/HorizontalScrollView02"
and:layout_width="fill_parent" and:layout_height="wrap_content">
<EditText and:id="#+id/EditText01"
and:layout_width="wrap_content" and:layout_height="wrap_content"
and:text="#+id/EditText01"/>
</LinearLayout>
<WebView and:id="#+id/uiContent"
and:layout_weight="1"
and:layout_width="fill_parent"
and:layout_height="fill_parent"/>
</LinearLayout>
And here is the Java code:
public class WebtagActivity extends Activity
{
// UI components.
private WebView mContent;
private EditText mUrl;
#Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContent = (WebView) findViewById(R.id.uiContent);
mContent.getSettings().setJavaScriptEnabled(true);
mContent.loadUrl("http://www.google.fr/");
}
}
Based on this very simple example, it's impossible to get focus on the WebView (i.e. the google search terms input box)! However when adding "mContent.requestFocus();" during initialization, things get better. I can switch from my EditText to my WebView if I click on WebView first only... But behaviour gets soon very "erratic", with WebView sometimes not getting focus when touching it, sometimes getting it but staying with a focus rectangle around WebView fields (orange or green depending on your phone) after another touch to go back on EditText component (in which I can write text)!
Anyway, a solution I found is to add a touchListener and request focus when clicking on a WebView:
mContent.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
This solves almost all problems (switching from classic components to WebView and writing text) except one: WebView keeps the focusRectangle (green or orange) around fields whether it has focus or not. In the below screenshot, my EditText has the focus and receive text but WebView still looks like it has focus. I tried clearFocus but that doesn't do anything:
See the result
Any idea how to solve this?
Thanks a lot for your help!
make sure that parent layout that extends "ViewGroup" doesn't have property
android:descendantFocusability = "blocksDescendants" which prevent child layout from getting focused
This helped my problem for focus:
Webview.requestFocus(View.FOCUS_DOWN|View.FOCUS_UP);
and this for sensitive touch:
Webview.getSettings().setLightTouchEnabled(true);
Just add android:focusable="true" to your WebView layout:
<WebView android:id="#+id/uiContent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"/>

Why does Android WebView display a black screen?

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.

Categories

Resources