ionutzm05
ionutzm05

Reputation: 53

Android WebView in different Tabs

I'm working on a project with "android.support.v4.view.ViewPager". There are 2 tabs and in each of these tabs I want to show a different WebView. Here is the code of one of those tabs and it's layout :

TabOne.java

public class TabOne extends Fragment
{
 WebView myWebView;
 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
        View view = inflater.inflate(R.layout.tabone, container, false);
            WebView myWebView = (WebView) view.findViewById(R.id.webview);
    myWebView.loadUrl("file:///android_asset/help/index.html");
    return view;
}
}

tabone.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/webview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" />

</LinearLayout>

The problem is that in the first tab, the WebView is loaded corectly. On the other Tab, which is exactly the same declared, I can't focus elements and I am getting some Force Closes sometimes. If I'm on the second tab, which has the error, and I switch the orientation to Landscape, the second tab is loaded perfectly but when I switch to the first tab I get the error. Here is the LogCat error :

11-18 09:00:37.460: E/webcoreglue(29444): Should not happen: no rect-based-test nodes found

Upvotes: 4

Views: 4604

Answers (3)

Audumbar
Audumbar

Reputation: 404

I have WebView inside a ViewPager and fixed this problem by overriding onTouchEvent() by extending WebView class.

Here is my code:

@Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) {
requestFocusFromTouch(); } return super.onTouchEvent(event); }

Cheers!

Upvotes: 0

Tuyen
Tuyen

Reputation: 83

In Java, create MyWebView as sub class of WebView

public class MyWebView extends WebView {

    public MyWebView(Context context) {
        super(context);
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        onScrollChanged(getScrollX(), getScrollY(), getScrollX(), getScrollY());
        return super.onTouchEvent(event);
    }
}

In layout xml, using MyWebView instead of WebView

<your.package.MyWebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006869

This appears to be a bug, either in ViewPager or WebView, on Android 4.1 (and perhaps higher).

Alas, I have no workaround for having WebView be on the second or subsequent pages of the pager.

Upvotes: 1

Related Questions