152

I'm trying to use the new Android P FloatingActionButton that's part of the com.google.android.material.floatingactionbutton.FloatingActionButton and I'm getting this warning:

VisibilityAwareImageButton.setVisibility can only be called from the same library group (groupId=com.google.android.material)

import com.google.android.material.floatingactionbutton.FloatingActionButton
import android.view.View

class MainActivity : AppCompatActivity() {

    lateinit var demoFab: FloatingActionButton

    override fun onCreate(savedInstanceState: Bundle?) {
        demoFab = findViewById(R.id.demoFab)
        demoFab.visibility = View.VISIBLE  // the warning is here
    }
}

enter image description here

I've tried searching and the only search result is in regards to responding to UI visibility changes:

Respond to UI visibility changes

I tried exploring how I could to see if there was a VISIBLE int value in that com.google.android.material package and the only one I found was com.google.android.material.floatingactionbutton.FloatingActionButton.VISIBLE, but the warning still remains.

Top-level build.gradle

buildscript {
    ext.kotlin_version = '1.2.41'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0-alpha14'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.gms:oss-licenses:0.9.2"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url "http://oss.sonatype.org/content/repositories/snapshots/" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Project-level build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'com.google.gms.oss.licenses.plugin'

android {
    compileSdkVersion 'android-P'
    defaultConfig {
        applicationId "com.codeforsanjose.maps.pacmap"
        minSdkVersion 21
        targetSdkVersion 'P'
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    splits {
        abi {
            enable true
            reset()
            include 'arm64-v8a', 'armeabi', 'armeabi-v7a', 'mips', 'x86', 'x86_64'
            universalApk false
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha2'

    implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:5.5.2'
    //implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:6.1.0'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.5.0'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.13.0'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.13.0'

    implementation 'com.google.android.gms:play-services-oss-licenses:15.0.1'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.squareup.moshi:moshi:1.5.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.squareup.retrofit2:converter-moshi:2.4.0'
    implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
}

edit:

I should note that I'm using Android Studio version 3.2 canary 14. It seems there have been some reported bugs for this version, and I suspect this is one of them.

edit 2:

Issue still exists with Android Studio version 3.2 canary 15, but I found a workaround for it using show() and hide()

override fun onCreate(savedInstanceState: Bundle?) {
    demoFab = findViewById(R.id.demoFab)
    demoFab.show()    // this works and doesn't have the warning
}
iknow
  • 8,358
  • 12
  • 41
  • 68
Kyle Falconer
  • 8,302
  • 6
  • 48
  • 68
  • Correct package is android.support.design.widget.FloatingActionButton – Pankaj Kumar May 15 '18 at 08:48
  • With Android P and Jetpack, the Fab was moved to this package, that's the one I'm trying to use. https://developer.android.com/reference/com/google/android/material/floatingactionbutton/FloatingActionButton.html – Kyle Falconer May 15 '18 at 13:05
  • @KyleFalconer Canary 15 is now available, check if the update can fix your problem :) – Levi Moreira May 18 '18 at 11:38
  • 2
    @PankajKumar Yup, still broken, but I did find another workaround for it (added "edit 2" to my question). – Kyle Falconer May 18 '18 at 18:36
  • 5
    This issue describes the bug: https://issuetracker.google.com/issues/115924750 Unfortunately, there is no solution or fix atm, so probably every one that is affected should star it. – ChristophK Jan 16 '19 at 09:19
  • I have the same problem when using Android Studio 3.5.2 / target API = 29 / AndroidX (library `com.google.android.material:material:1.0.0`) – Guillaume Husta Nov 08 '19 at 21:11

8 Answers8

277

Using Method 1

demoFab.show(); // in place of visible
demoFab.hide(); // in place of Invisible suppress the warning/error for me.

and Method 2

@SuppressLint("RestrictedApi") // also suppressed the warning
private void setUp() {
    ....
}

update:

Method 3:

demoFab.setVisibility(View.GONE);
demoFab.setVisibility(View.INVISIBLE);
demoFab.setVisibility(View.VISIBLE);

Method 4:

demoFab.visibility = View.GONE
demoFab.visibility = View.INVISIBLE
demoFab.visibility = View.VISIBLE
Akhila
  • 3,235
  • 1
  • 14
  • 30
  • 1
    hi, is there GONE and INVISIBLE methods? – Siarhei Jun 17 '19 at 11:20
  • Using .show() can remove the warning, but there's one problem: if the FAB has set,for example in XML, android:alpha="0.5" for transparency, the transparency not working then. How to fix this problem? Instead, I will prefer to use demoFab.visibility = View.VISIBLE if .show() can't work on android:alpha – Fisher Aug 02 '19 at 13:10
  • methods show and hide are perfect for standard floating action buttons. – devpelux Dec 15 '19 at 21:22
10

Seems to work fine just to cast it to a view.

(mFloatingActionButton as View).visibility = INVISIBLE

Of course you need to remember that the visibility may affect other components, so you should probably use show() and hide() at the same time to make sure other components are notified of the change.

Andreas
  • 920
  • 1
  • 10
  • 22
6

Use:

 myButton.hide();
 myClearButton.hide();

A typical example would be:

Hiding and showing buttons when user is typing or has focus on a EditText resource:

check if user is typing or has focus:

 mCommentField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus) {
                //user has focused
                showBts();

            } else {
                //focus has stopped perform your desired action
                hideButtons();
            }
        }


    });

Hide and Show button methods:

private void hideButtons() {

    mCommentButton.hide();
    mClearButton.hide();
}

private void showBts() {

    mCommentButton. show();
    mClearButton.show();

And in your xml, set the buttons to invisible by default so that they only display/show when a user has focus or is typing:

android:visibility="invisible"

Best practice:

 android:visibility="Gone"

Using visibility gone means your view is doesn't take up any space on your layout while "invisible" will take up unnecessary space on your layout

In this example:My Views are in a ViewHolder and iam referencing the buttons from a a fragment with a recylerview

RileyManda
  • 2,536
  • 25
  • 30
3

This also works:

findViewById(R.id.fab).setVisibility(View.GONE);
vcdo
  • 31
  • 5
  • Yes it does.I was using a fragment in my example:setVisibility(View.GONE); does not always work in fragments especially when you have declared your views in a viewHolder that's in a separate class of its own and you are merely referencing from the Viewholder. – RileyManda Mar 18 '19 at 07:48
1

For me above methods did not work so I created an extension function to make it work.

fun View.showView() {
    this.visibility = View.VISIBLE
}

fun View.hideView() {
    this.visibility = View.GONE
}

now call like

binding.fabAdded.showView()
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
0

For Kotlin I have an extension method

fun viewsVisibility(visibility: Int, vararg views: View) {
    for (view in views) { view.visibility = visibility }
}

Then in then in the code you can do the following

viewsVisibility(View.VISIBLE, demoFab) 
viewsVisibility(View.GONE, demoFab)
viewsVisibility(View.INVISIBLE, demoFab, addFab, removeFab)

The error will be gone and this gives the flexibility for any visibility state along with taking in a list of views to handle. There are a lot of times I need to handle more than one view at a time as shown in the final example line.

Kevin
  • 1,405
  • 15
  • 16
0
if(data){
            fragmentPendingApprovalDetailsBinding.fabPendingList.show();
        }else {
            fragmentPendingApprovalDetailsBinding.fabPendingList.hide();
        }
Soumen Das
  • 1,292
  • 17
  • 12
0

For com.google.android.material.floatingactionbutton.FloatingActionButton

Visibility

Use the show and hide methods to animate the visibility of a FloatingActionButton. The show animation grows the widget and fades it in, while the hide animation shrinks the widget and fades it out.

Source: https://material.io/develop/android/components/floating-action-button/

For android.support.design.widget.FloatingActionButton use setVisibility() Method

tanni tanna
  • 544
  • 4
  • 12