Android P visibilityawareimagebutton.setVisibility can only be called from the same library group

半城伤御伤魂 提交于 2019-11-29 22:44:01

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);

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.

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

This also works:

findViewById(R.id.fab).setVisibility(View.GONE);

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.

if(data){
            fragmentPendingApprovalDetailsBinding.fabPendingList.show();
        }else {
            fragmentPendingApprovalDetailsBinding.fabPendingList.hide();
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!