Search code examples
javaandroidfloating-action-button

Not working any solution to change a fab color programmatically


I have to change fab color on click from blue to orange. I tried by myself but it wasn't working so i searched for an answer here on stack and came across some solution they were proposing but didn't work for me.

One of the question's i've came across was this:

so i've tried using:

fab.setBackgroundColor(Color.parseColor("#ffffff"));

fab.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#33691E"));

mFab.setBackgroundTintList(ColorStateList.valueOf(getResources.getColor(R.color.mColor)));

    private void onClickStart(View v) {
        int colorArancione = getResources().getColor(R.color.Arancione);

        if(mStatoScanButton) {
                mProgressBar.setVisibility(View.VISIBLE);
                mScanButton.setColorFilter(colorArancione);
                mScanButton.setImageDrawable(getDrawable(R.drawable.ic_add));
                //mScanButton.setForegroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.Arancione)));
                mStatoScanButton=false;

            startTimedScan();
        }else if(!mStatoScanButton){
            mProgressBar.setVisibility(View.GONE);
            mScanButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.Blue)));
            mStatoScanButton=true;
            if(bluetoothDevicesScansionati.size() == 0)
                mTestoTextView = "Nessun dispositivo trovato";
            fragment_dispositiviScansionati.updateTexto();

            try {
                mTio.stopScan();

            } catch (InvalidObjectException e) {
                e.printStackTrace();
            }
        }
    }


<android.support.design.widget.FloatingActionButton
            android:id="@+id/scanButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClickStart"
            android:backgroundTint="@color/Blue"
            android:src="@drawable/ic_search_black_24dp"
            android:fontFamily="@font/lustria"
            android:elevation="2dp"
            android:layout_gravity="center"
            app:maxImageSize="35dp"
            app:fabCustomSize="70dp"
            app:borderWidth="0dp"
            />

Solution

  • In your xml file you should use the property app:backgroundTint="@color/Blue" instead of android:backgroundTint="@color/Blue" like this:

    <android.support.design.widget.FloatingActionButton
                android:id="@+id/scanButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onClickStart"
                app:backgroundTint="@color/Blue"
                android:src="@drawable/ic_search_black_24dp"
                android:fontFamily="@font/lustria"
                android:elevation="2dp"
                android:layout_gravity="center"
                app:maxImageSize="35dp"
                app:fabCustomSize="70dp"
                app:borderWidth="0dp"
                />
    

    And Then you can use this method:

    fab.setBackgroundTintList(ColorStateList.valueOf(#33691E)));
    

    Because this method don't work with the property "android:"