I have a Fragment
where I display the WebView
. The Activity
containing the Fragment has CoordinatorLayout
as its parent. I want to collapse the Toolbar
when the user scrolls the WebView
.
Activity layout
<android.support.design.widget.CoordinatorLayout android:id="@+id/cordinator_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/topic_tile_height"
app:layout_behavior="android.support.design.widget.NewshuntAppBarLayoutBehavior">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/topic_tile_collapsing_toolbar"
app:layout_scrollFlags="scroll|enterAlways">
<include layout="@layout/actionbar_news"/>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/category_tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/topic_slidingtablayout_height"
android:layout_gravity="bottom"
android:contentDescription="@string/newspaper_category_tabs"
android:paddingTop="@dimen/topics_sliding_tab_margin_top"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/news_list_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:contentDescription="@string/categories_pager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
The recommended solution is to use WebView inside NestedScrollView. The XML layout for the fragment is as follows.
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/category_webitem_container"
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.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<WebView
android:id="@+id/web_item_content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</WebView>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
This works fine when the response to be loaded in the WebView is simple HTML. But if the response contains an iframe element, then the WebView only fills the view port. I am not able to scroll the WebView
.
Here is the sample response where WebView does not scroll. Here the height is coming as 100%. If I get a fixed height like 1000dp or something, I am able to scroll the WebView. But I won't get a fixed height from the server.
"content":"<html>\r\n<head>\r\n\t<title></title>\r\n</head>\r\n<body>\r\n<div><iframe height=\"100%\" src=\"https://sportscafe.in/s3/sc-s3-static-mum.sportscafe.in/kwc/en_fixtures.html\" width=\"100%\"></iframe></div>\r\n</body>\r\n</html>\r\n"
Although there is more content in the WebView, I am not able to scroll it beyond the text March 29