I\'m trying to use the new Android P FloatingActionButton that\'s part of the com.google.android.material.floatingactionbutton.FloatingActionButton
and I\'m get
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
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
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.
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()
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.
if(data){
fragmentPendingApprovalDetailsBinding.fabPendingList.show();
}else {
fragmentPendingApprovalDetailsBinding.fabPendingList.hide();
}