How to make an actionbar like google play that disappears when scrolling

How to make a transparent or translucent ActionBar like Google Play that fades or scrolls when scrolling with windowActionBarOverlay ?

Check out the following screenshots

** enter image description here **

+58
android android-actionbar transparent android-actionbar-compat
Aug 21 '14 at 11:09 on
source share
8 answers

Below is the code that I used in the application that I am working on

You will need to use the OnScrollChanged function in the ScrollView . ActionBar does not allow you to set the opacity, so set the background image on the action bar, and you can change its opacity based on the amount of scroll in the scroll. I gave an example of a workflow

Feature sets specify the appropriate alpha for the locationImage based on its WRT position window.

this.getScrollY() gives you how many ScrollView

 public void OnScrollChanged(int l, int t, int oldl, int oldt) { // Code ... locationImage.setAlpha(getAlphaForView(locationImageInitialLocation- this.getScrollY())); } private float getAlphaForView(int position) { int diff = 0; float minAlpha = 0.4f, maxAlpha = 1.f; float alpha = minAlpha; // min alpha if (position > screenHeight) alpha = minAlpha; else if (position + locationImageHeight < screenHeight) alpha = maxAlpha; else { diff = screenHeight - position; alpha += ((diff * 1f) / locationImageHeight)* (maxAlpha - minAlpha); // 1f and 0.4f are maximum and min // alpha // this will return a number betn 0f and 0.6f } // System.out.println(alpha+" "+screenHeight +" "+locationImageInitialLocation+" "+position+" "+diff); return alpha; } 

EDIT: I added a working example example at https://github.com/ramanadv/fadingActionBar , you can watch it.

enter image description here

+31
Aug 21 '14 at 12:05
source share

Please browse this https://github.com/ManuelPeinado/FadingActionBar library, which implements the effect effect of the effect fading that you want

+8
Oct 22 '14 at 5:18
source share
+8
Apr 14 '15 at 10:14
source share

White Paper Google DeveloperDocumentation

enter image description here

overlay action bar.

Enable Blend Mode

To enable blending mode for the action bar, you need to create a custom theme that extends the existing action bar theme and sets the android: windowActionBarOverlay property to true.

Only for Android 3.0 and above

If your minSdkVersion is set to 11 or higher, your custom theme should use the Theme.Holo theme (or one of its descendants) as the parent theme. For example:

 <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@android:style/Theme.Holo"> <item name="android:windowActionBarOverlay">true</item> </style> </resources> 

For Android 2.1 and higher

If your application uses the support library for compatibility on devices with versions lower than Android 3.0, your custom theme should use Theme.AppCompat (or one of its descendants) as the parent theme. For example:

 <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@android:style/Theme.AppCompat"> <item name="android:windowActionBarOverlay">true</item> <!-- Support library compatibility --> <item name="windowActionBarOverlay">true</item> </style> </resources> 

Specify the upper limit of the layout.

When the action bar is in overlay mode, it can obscure the portion of your layout that should remain visible. To ensure that such items remain under the action bar all the time, add an edge or pad to the top of the view (s) using the height specified by actionBarSize. For example:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="?android:attr/actionBarSize"> ... </RelativeLayout> 
+6
Aug 22 '14 at 4:18
source share

There is official documentation on how to achieve this effect: https://developer.android.com/training/basics/actionbar/overlaying.html

Edit: There is an open source library of ObservableScrollView with a lot of open source demos with various ActionBar effects, including the ones you mentioned. This is a great resource: https://github.com/ksoichiro/Android-ObservableScrollView .

+3
Feb 06 '15 at 23:17
source share

If you use the Layout coordinator in your xml layout, you should check out this article describing how to handle scrolls and respond to other views.

There is an example of doing what you asked if you add your image as a child of CollapsingToolbarLayout, all magic is handled automatically, and you can even have some options, such as canvas color, minimum height to start collapsing, etc.:

dTDPztp.png

+2
Apr 05 '17 at 3:26
source share

Using the AbsListView scroll listener, we can achieve for a list simply without using another complex library or ScrollView

set scroll listener to watchlist

 public class PagedScrollListener implements AbsListView.OnScrollListener { private ActionBar mActionBar; private View mTopHideView; // represent header view @Override public void onScrollStateChanged(final AbsListView view, final int scrollState) { mScrollState = scrollState; } @Override public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) { if (mActionBar != null && mTopHideView != null && mListView != null) { Log.i(TAG, getScrollY() + ""); int currentY = getScrollY(); final int headerHeight = mTopHideView.getHeight() - mActionBar.getHeight(); final float ratio = (float) Math.min(Math.max(currentY, 0), headerHeight) / headerHeight; final int newAlpha = (int) (ratio * 255); Log.i(TAG, newAlpha + " alpha"); mActionBarDrawaqble.setAlpha(newAlpha); }} public void setActionBarRefernce(ActionBar actionBar, View topHideView, float actionBarHeight, ListView listView) { mActionBar = actionBar; mActionBarHeight = actionBarHeight; mTopHideView = topHideView; mListView = listView; mActionBarDrawaqble = new ColorDrawable(ContextCompat.getColor(mContext, R.color.google_plus_red)); mActionBar.setBackgroundDrawable(mActionBarDrawaqble); mActionBarDrawaqble.setAlpha(0); } public int getScrollY() { View c = mListView.getChildAt(0); if (c == null) { return 0; } int firstVisiblePosition = mListView.getFirstVisiblePosition(); int top = c.getTop(); int headerHeight = 0; if (firstVisiblePosition >= 1) { headerHeight = mTopHideView.getHeight(); } return -top + firstVisiblePosition * c.getHeight() + headerHeight; } } 

// Note: Remember to call the ** setActionBarRefernce ** method of the user listener

0
Dec 28 '15 at 11:55
source share

In webView:

 @JavascriptInterface public void showToolbar(String scrollY, String imgWidth) { int int_scrollY = Integer.valueOf(scrollY); int int_imgWidth = Integer.valueOf(imgWidth); String clr; if (int_scrollY<=int_imgWidth-actionBarHeight) { clr = Integer.toHexString(int_scrollY * 255 / (int_imgWidth-actionBarHeight)); }else{ clr = Integer.toHexString(255); } toolbar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#"+clr+"9CCC65"))); } 
-6
Dec 05 '14 at 5:39
source share



All Articles