Rikki Tikki Tavi
Rikki Tikki Tavi

Reputation: 3139

Material Design : how to set transparency for Android Floating Action Button

My question is related to this one.

When I open the NavigationDrawer, the Floating Button is on the top of it but has to be below.

I try to do it like that :

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            Log.i(TAG , " inner onDrawerSlide");
            super.onDrawerSlide(drawerView, slideOffset);
            fabButton.setAlpha(25);
            float alpha = 0.2f;
            AlphaAnimation alphaUp = new AlphaAnimation(alpha, alpha);
            alphaUp.setFillAfter(true);
            fabButton.startAnimation(alphaUp);
            syncState();
        }

and like that :

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerClosed(view);
                invalidateOptionsMenu();
                fabButton.setAlpha(255);
                syncState();
            }

Nothing worked for me. What can be the solution ?

My layout:

<mobapply.freightexchange.customviews.FragmentNavigationDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <!-- The ActionBar -->
    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<ListView
    android:id="@+id/lvDrawer"
    android:layout_width="match_parent"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:background="#FFFFFF"
    android:cacheColorHint="@android:color/transparent"
     />

FragmentNavigationDrawer is the custom DrawerLayout

Upvotes: 6

Views: 7334

Answers (4)

Prashant
Prashant

Reputation: 1056

I got it working with this line of code, but it may have an issue with the change in orientation of device.

public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);

            fab.setAlpha(1-slideOffset);
        }

Upvotes: 0

Avinash Maurya
Avinash Maurya

Reputation: 1

if the position of the fab is problem then make it draggable . here is the code for that

private FloatingActionButton fab;
float dX;
float dY;
int lastAction;


fab = (FloatingActionButton) findViewById(R.id.fab);

    fab.setOnTouchListener(this);





@Override
public boolean onTouch(View view, MotionEvent event) {
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            dX = view.getX() - event.getRawX();
            dY = view.getY() - event.getRawY();
            lastAction = MotionEvent.ACTION_DOWN;
            break;

        case MotionEvent.ACTION_MOVE:
            view.setY(event.getRawY() + dY);
            view.setX(event.getRawX() + dX);
            lastAction = MotionEvent.ACTION_MOVE;
            break;

        case MotionEvent.ACTION_UP:
            if (lastAction == MotionEvent.ACTION_DOWN) {
                Intent intent = new Intent(Main.this, Cart.class);
                startActivity(intent);
                Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
            }
            break;

        default:
            return false;
    }
    return true;
}

Upvotes: 0

Nikhil Bawane
Nikhil Bawane

Reputation: 3

This should hide it with a nice animation :

public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                invalidateOptionsMenu();
                fabButton.setAlpha(1 - slideOffset);
                syncState();
            }

Or, you could slide it out of view :

public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                invalidateOptionsMenu();
                fabButton.setTranslationX(slideOffset * 100);
                syncState();
            }

Upvotes: 0

Bartek Lipinski
Bartek Lipinski

Reputation: 31438

From the Android documentation for setAlpha(float alpha) (since API 11):

Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is completely transparent and 1 means the view is completely opaque.

You're trying to set it to value 25 or 255 :)

Upvotes: 5

Related Questions