Friday, August 12, 2016

HOW TO HIDE AND SHOW FLOATING ACTION BUTTON WHILE SCROLLING THE LIST

1- Add following code to your onCreate Method:-
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.activity_listview, mobileArray);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
Animation makeInAnimation = AnimationUtils.makeInAnimation(getBaseContext(), false);
makeInAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) { }
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationStart(Animation animation) {
fab.setVisibility(View.VISIBLE);
}
});
Animation makeOutAnimation = AnimationUtils.makeOutAnimation(getBaseContext(), true);
makeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
fab.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationStart(Animation animation) { }
});
// ...
if (fab.isShown()) {
fab.startAnimation(makeOutAnimation);
}
if (!fab.isShown()) {
fab.startAnimation(makeInAnimation);
}
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
fab.show();
}
@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
fab.hide();
}
});

2- Add follwing line in xml to get tag
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/mobile_list"
app:layout_anchorGravity="bottom|right|end" />


3- Activity_list.xml view

    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >

Default
Image1

OnScroll
Image2

After Scroll
Image3

No comments: