Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a ActionBar like Google Play that fades in when scrolling

How to make transparent or translucent ActionBar like Google Play that fades in or out when scrolling using windowActionBarOverlay?

Check the following screenshots

**enter image description here**

like image 873
Cristiana Chavez Avatar asked Aug 21 '14 11:08

Cristiana Chavez


People also ask

How do I create my own action bar?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is action bar android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

What is scrolling activity in Android Studio?

Scrolling activity is an important thing to have in your app as it gives your user a perfect view when the layout is long. The Scrolling activity can be implemented easily in android studio project because android studio gives a ready to use activity.


2 Answers

The following is the code I used in the app I am working

You will have to use the OnScrollChanged function in your ScrollView. ActionBar doesn't let you set the opacity , so set a background drawable on the actionbar and you can change its opacity based on the amount of scroll in the scrollview. I have given an example workflow

The function sets gives the appropriate alpha for the view locationImage based on its position WRT window .

this.getScrollY() gives you how much the scrollView has scrolled

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 have added an example working sample at https://github.com/ramanadv/fadingActionBar , you can have a look at it.

enter image description here

like image 175
CommandSpace Avatar answered Sep 28 '22 03:09

CommandSpace


Please check this library https://github.com/ManuelPeinado/FadingActionBar which implements the cool fading action bar effect that you want

like image 36
shaobin0604 Avatar answered Sep 28 '22 02:09

shaobin0604