0

After read various stackoverflow's questions about floatingactionbutton, like here Android changing Floating Action Button color which is one of most complete questions about it, and tried various suggestions, I am using fab over a Fragment in an app with compileSdkVersion 33 , targetSdkVersion 33 , implementation 'androidx.appcompat:appcompat:1.5.1' , plus I settled last version of Material component implementation 'com.google.android.material:material:1.7.0' , and in fragment xml I settled as :

<com.google.android.material.floatingactionbutton.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    app:srcCompat="@android:drawable/ic_popup_sync"
    app:tint="@color/white"
    app:backgroundTint="@color/fab_enabled"/>

and in code I tried various ways, as you can see on commented code lines, but seems fab doesn't change background color as disabled even the state corresponds as disabled :

FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        fab.setEnabled(false);
        //fab.getBackground().set(getContext().getColor(R.color.fab_disabled));
        //fab.setBackgroundTintList(ColorStateList.valueOf(getContext().getColor(R.color.fab_disabled)));
        //fab.setBackgroundColor( getContext().getColor(R.color.fab_disabled) );
        fab.getBackground().mutate().setTint(getContext().getColor(R.color.fab_disabled));

does maybe that depend from the material version? Or is maybe related to some refresh needed to show background color changed? Or even because I used a standard java button ( @android:drawable/ic_popup_sync ), not a custom one? .... I also tried using a selector instead of color in backgroundTint too to make it changes automatically depending from the current state, but it didn't worked...

Edit 2023-01-11 :

I tried to incorporate the listview and the floating action button inside a RelativeLayout to try to see listview cleared as like as I call adapter.clear() and adapter.notifyDataSetChanged() :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/fr_myfragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyFragment">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

                <!-- TODO: Update blank fragment layout -->
                <ListView
                    android:id="@+id/myfragment_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_margin="5dp" />

                <com.google.android.material.floatingactionbutton.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentBottom="true"
                    android:layout_gravity="bottom|end"
                    android:layout_margin="16dp"
                    android:backgroundTint="@color/fab_colors"
                    android:src="@android:drawable/ic_popup_sync" />

        </RelativeLayout>

</FrameLayout>

but once I click fab button the listview won't clear before to do new search of items to add in listview, neither hide the fab button itself... I tried also to change setOnClickListener with setOnTouchListener for fab button to see if fab's behavior could change but doesn't work... And I'm really stuck on it..... any direction would be appreciate.... Thanks!

Luigino
  • 745
  • 3
  • 7
  • 26
  • What actually do you need? Are you just need to change background color? – Sohaib Ahmed Jan 07 '23 at 11:38
  • Hello @SohaibAhmed , yes exactly... as you see in the code I pasted I settled to false the button so I can't click and I try to change background color to show the aspect as disabled (grey color that's usually used to make object disabled)... – Luigino Jan 07 '23 at 12:15
  • Kindly check my answer and let me know if issue is resolved? – Sohaib Ahmed Jan 07 '23 at 12:33

2 Answers2

0

Try this code..

fab.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.fab_disabled))

Here this is context

Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23
  • Hello @SohaibAhmed , I tried already that way as you can see in commented code lines but I see in Debugger the attribute `mColor` before to call `setBackgroundTintList` it contained value `-8934372` which should be the enabled color state and after called `setBackgroundTintList` it was `-2763307` so that should mean it has changed the color but maybe just it didn't refreshed the interface because right after changed state I am executing some operation threads...is there a way to force refresh after changed color but being still inside onClick event?.... Thanks – Luigino Jan 07 '23 at 17:44
  • I just tried also to hide fab button as alternative with `fab.hide();` then showing it back once finished but it didn't disappeared once called the method... is maybe because this fab is placed in a `FrameLayout` which maybe it doesn't work well?.... – Luigino Jan 08 '23 at 09:20
  • Hello everyone! I tried also to add an indeterminate ProgressBar but once I press FAB button it doesn't show once pressed and hide after completed operations in onClick so either still trying to change fab state (changing background or hiding) right after clicking button it doesn't work.... even doesn't work clearing listview right after fab is clicked... like all operations are applied on the interface only after exits from onCLick event but I don't want that.... any direction would be appreciate! Thanks – Luigino Jan 10 '23 at 16:59
0

At the end after some tests, I figured it out putting the code in a asynchronous runnable thread passing fab button so , once it finished, I can re-enable the button. Thanks to everyone anyway for suggests!

Luigino
  • 745
  • 3
  • 7
  • 26