android - CoordinatorLayout/NestedScrollView/Hide-Show Toolbar/Issue with WebView - android

I have a problem with that :
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
When I scrolls in the webview, the toolbar is hidden or shown (perfect !) but there is a problem with the loading / positioning web pages.
For example, if I scrolls to the middle of a page and I click on a link, the new page that will load also be located at approximately the middle of the page instead of on top. As if scrollbars were not moving from one page to another.
if I add to the NestedScrollView:
android:fillViewport="true"
everything works with the webview (pages load and appear well although starting from the top) but I lose the Hide/Show with the toolbar :(
Do you have any idea about this problem?
Thank you in advance for your help :)
(For information : Android Design Support Library : 23.0.1)
Yop

My assumption:
Since you are putting the WebView inside a NestedScrollView the scrolling is not done on the WebView level so when you load a new page the NestedScrollView stays in the same position.
Suggestion:
create a WebViewClient and override onPageStarted here you should change the NestedScrollView scroll position to 0:
nestedScrollView.scrollTo(0, 0);

Related

How to hide ToolBar when I scrolling content up?

I am trying to hide my tool bar when I scroll my text and image with content. Here I use scrollView for getting scroll content. When I scroll content up, how to hide the tool bar?
Here is my XMl code:
content_main.XML:
<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingTop="?android:attr/actionBarSize"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/textone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="23dp"
android:textStyle="bold"
android:text="hello world jheds sdjhs jds sjbs skjs ksjs kksjs ksj sdd dskd js sk "/>
<ImageView
android:id="#+id/imge"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="#drawable/imag_bg"/>
<TextView
android:id="#+id/texttwo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Pretty good, the Toolbar is moving along with the list and getting back just as we expect it to. This is thanks to the restrictions that we put on the mToolbarOffset variable.
If we would omit checking if it’s bigger than 0 and lower than mToolbarHeight then when
we would scroll
up our list, the Toolbar would move along far away off the screen, so to show it back you
would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
position and not more so it’s “sitting” right above the list all of the time and if we
start scrolling down, we can see it immediately showing.
up our list, the Toolbar would move along far away off the screen, so to show it back you
would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
position and not more so it’s “sitting” right above the list all of the time and if we
start scrolling down, we can see it immediately showing
up our list, the Toolbar would move along far away off the screen, so to show it back you
would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
position and not more so it’s “sitting” right above the list all of the time and if we
start scrolling down, we can see it immediately showing
up our list, the Toolbar would move along far away off the screen, so to show it back you
would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
position and not more so it’s “sitting” right above the list all of the time and if we
start scrolling down, we can see it immediately showing
up our list, the Toolbar would move along far away off the screen, so to show it back you
would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
position and not more so it’s “sitting” right above the list all of the time and if we
start scrolling down, we can see it immediately showing
up our list, the Toolbar would move along far away off the screen, so to show it back you
would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
position and not more so it’s “sitting” right above the list all of the time and if we
start scrolling down, we can see it immediately showing
It works pretty well, but this is not what we want. It feels weird that you can
stop it in the middle of
the
scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
Games app
which I consider as a bug
It works pretty well, but this is not what we want. It feels weird that you can
stop it in the middle of
the
scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
Games app
which I consider as a bug
It works pretty well, but this is not what we want. It feels weird that you can
stop it in the middle of
the
scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
Games app
which I consider as a bug."/>
</LinearLayout>
<View
android:layout_width="wrap_content"
android:layout_height="30dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="hai"
android:layout_width="160dp"
android:layout_height="match_parent" />
<Button
android:text="hello"
android:layout_width="160dp"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
activity_main.XML
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
you have to do many changes in your both layout. first use CoordinatorLayout in activity_main.XML like below(change theme as per your requirement).
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
in content_main.XML use android.support.v4.widget.NestedScrollView instead of ScrollView.
also use app:layout_behavior="#string/appbar_scrolling_view_behavior" inside android.support.v4.widget.NestedScrollView like below.
<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hello world jheds sdjhs jds sjbs skjs ksjs kksjs ksj sdd dskd js sk "
android:textSize="25dp"
android:textStyle="bold" />
/// Add your other code here
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
This is the best scenario to make use of CoordinatorLayout in your app. CoordinatorLayout is a super-powered FrameLayout which has got a lot of nifty animation tricks upon its sleeves.
The Design library introduces CoordinatorLayout, a layout which
provides an additional level of control over touch events between
child views, something which many of the components in the Design
library take advantage of.
You can start with this and this tutorial.
Wrap activity_main.xml in Coordinator Layout so it will be its parent layout.
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
You can find my solution about your question from here:
Android Toolbar + Tab Layout + Drawer, Hide toolbar when scrolling and take TabLayout to the top
This's a working solutio but it's not the best way to implement this animation. With CoordiantorLayout you can relate your views and it's scrolling behaviors.
You can find more info from here: https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html
When i have time i'll try to post a code example for you.
Just set flag to like app:layout_scrollFlags="scroll|enterAlways"
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#FFFFFF"
app:layout_scrollFlags="scroll|enterAlways"
/>

SupportMapFragment inside of ViewPager with ScrollingViewBehavior inside of CoordinatorLayout jumps on render

Here is my layout.
The main idea is a ViewPager with few fragments and action bar above.
I sucessfully added a fragment with a SupportMapFragment inside of it.
But I ran into a problem in android 4.0.4.
When I use this layout below, my map frequently jumps over AppBarLayout. When jump occurs, I can see black rectangle on bot and it's size visually equal to AppBarLayout size.
These jumps occur with every map update(zooming, moving and markers add/edit/remove).
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activities.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/AppTheme.TabLayout" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
What I've tried:
If I remove
app:layout_behavior="#string/appbar_scrolling_view_behavior"
then my map fills all the view space(expected result) and no jumping
occurs.
If I change CoordinatorLayout with LinearLayout, jumping stops as well.
But I want to save features of CoordinatorLayout, so the 2nd way doesn't fit all my needs. The first one requires me to calculate top offset and or hardcode top margin, what I don't think is a good idea.
I suppose, that the problem is, when map updates it places in (0,0) and then
with this
app:layout_behavior="#string/appbar_scrolling_view_behavior"
is moved to right position. But I am not quite sure.
Maybe someone has already met this problem and can advise me how can I fix map jumping issue?

android Place content below toolbar

I have this layout:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_favorite_outline_white_24dp"/>
</android.support.design.widget.CoordinatorLayout>
Setting #string/appbar_scrolling_view_behavior attribute shifts the RecyclerView by the height of the Toolbar.
But what if I need the first element of the RecyclerView to be aligned to the status bar.
I want the Toolbar to cover (be above) the first element.
In other words, I don't want any offset which #string/appbar_scrolling_view_behavior behaviour entails.
Could you please tell me how to do that?
I had the same problem and I just wrapped the AppbarLayout and the rest of my views (in your case the recyclerview) in a RelativeLayout and it works fine. I don't know if there are any downsides with that approach.
The offset is not by #string/appbar_scrolling_view_behavior behaviour it's due to AppBarLayout it pushes the content down.
I'm not sure if there is any other better solution. But I'd suggest to remove the AppBarLayout to have your content go under the Toolbar. Moreover you might be need the scrolling behaviour for that you can check the library below.
It's been used by lots of apps like Jair Music Player even WhatsApp too uses it.
Library:
Android Observable Scroll View

Content behind CoordinatorLayout AppBarLayout

I was creating a settings activity/layout for my app. I have a CoordinatorLayout with an AppBarLayout and Toolbar, then beneath that it includes content_settings.xml. When the content loads the .xml file is behind the app bar.
I'm using this same setup to load the main content and it works fine, but for some reason isn't rendering correctly within the Settings section.
activity_settings.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_setting" />
The content_settings.xml is just a FrameLayout that is replaced by a PreferenceFragment
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/settings_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
add this to your Recyclerview :
app:layout_behavior="#string/appbar_scrolling_view_behavior"
In my case the view under the toolbar wasn't scrollable so even though the accepted answer did stop the overlapping it pushed the content down by the height of the toolbar, pushing elements offscreen.
The solution in this case was to also remove the
app:layout_scrollFlags
from the Toolbar that I was including/sharing with other layouts that had scrolling views.

Crosswalk's XWalkView (an alternative to Webview) not working with AppBarLayout + CoordinatorLayout

I'm trying to achieve the scrolling Toolbar technique (hiding Toolbar when scrolling down in a sibling child view) using CoordinatorLayout and AppBarLayout.
I'm using Crosswalk's XWalkView (it's a view which functions like WebView).
When using regular WebView (inside a NestedScrollView) I can achieve the wanted effect:
<android.support.design.widget.CoordinatorLayout
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/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webViewOrig"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
However when using the XWalkView in the same way, I can't get it to work.
After using the following XML layout:
<android.support.v4.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport = "true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<org.xwalk.core.XWalkView
android:id="#+id/webViewCross"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.NestedScrollView>
the view doesn't even appear.
When removing the following line
app:layout_behavior="#string/appbar_scrolling_view_behavior"
the XWalkView is at least displayed but on the whole screen and on top of the Toolbar.
What should I do to make it work? Since it's an open-source I should be able to make it work... I suspect it's either subclass XWalkView and implement some interface (NestedScrollingChild?) or creating a custom behaviour by subclassing CoordinatorLayout.Behavior or AppBarLayout.ScrollingViewBehavior.
Any ideas?
BTW XWalkView is using internally either SurfaceView or TextureView to render the web content.

Categories

Resources