2

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"
            />

2 Answers2

4

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:"

Gian9751
  • 91
  • 8
0

The document suggests that it takes the @color/accent by default. But we can override it on code by using

fab.setBackgroundTintList(ColorStateList)

but remember,

The minimum API version to use this library is 15 so you need to update it! if you don't want to do it

AhmedSaleh
  • 21
  • 1
  • 6