Shadow problem Floating Action Button (api = 19) - android

I had a problem with FAB shadow on API <= 19. I used compatElevation= 0f and shadow disappeared. But it appears again during onClick and looks strange:
XML code:
<android.support.design.widget.FloatingActionButton
android:id="#+id/RateBtn"
android:layout_width="#dimen/backet_Btn_size"
android:layout_height="#dimen/backet_Btn_size"
android:elevation="0dp"
app:elevation="0dp"
android:clickable="true"
android:focusable="true"
android:src="#drawable/star_rate2"
app:backgroundTint="#color/w"
app:layout_constraintEnd_toEndOf="#+id/langBtn2"
app:layout_constraintStart_toStartOf="#+id/langBtn2"
app:layout_constraintTop_toTopOf="#+id/SettingsBtn"
app:rippleColor="#color/grey_700" />
Code:
var fb = view?.findViewById(R.id.RateBtn) as FloatingActionButton
val lp =it.layoutParams
if (lp !=null){
lp.width= size_fb
lp.height=size_fb
it.customSize=size_fb
it.layoutParams= lp
}
I think there is some problem when I try to change FAB size programmatically. When I set size in xml, everything is ok

use this code
android:elevation="0dp"
app:elevation="0dp"

Override the default elevation of the FAB by adding
android:elevation="0dp"

Java
setStateListAnimator(null);
XML
android:stateListAnimator="#null"

Related

fab isn't being anchored afted calling onBackPressed() [duplicate]

I am having an issue with my FabCradleMargin becoming less, almost flat, inside my Bottom App Bar when navigating through my app and scrolling up/down while hideonScroll is set to true. When the BottomAppBar hides from the screen, it returns resized under the Floating action Button. Must be a glitch in the new Android Material Components. Has anyone else been experiencing this issue. If so, what suggestions do you have to fixing it.
and
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
app:elevation="4dp"
app:fabAlignmentMode="center"
app:fabCradleRoundedCornerRadius="2dp"
app:hideOnScroll="true"
app:layout_scrollFlags="scroll|enterAlways"
app:navigationIcon="#drawable/ic_action_list" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/blue500"
app:fabSize="normal"
app:layout_anchor="#+id/bar"
app:tint="#color/white"
app:layout_anchorGravity="right"
app:srcCompat="#drawable/ic_select_camera" />
I stumbled upon this problem as well. In my case it depended on the way I tried to hide the BottomAppBar and the FloatingActionButton. This is what I had first (Kotlin):
private fun showBottomNavigationBar(barVisibility: Boolean, fabVisibility: Boolean) {
navView.visibility = if (barVisibility) BottomAppBar.VISIBLE else BottomAppBar.GONE
fab.visibility = if (fabVisibility) FloatingActionButton.VISIBLE else FloatingActionButton.GONE
}
And this is what fixed it:
private fun showBottomNavigationBar(barVisibility: Boolean, fabVisibility: Boolean) {
navView.visibility = if (barVisibility) BottomAppBar.VISIBLE else BottomAppBar.GONE
if (fabVisibility) fab.show() else fab.hide()
}
So instead of hiding the FloatingActionButton with the visibility property, I used the hide() and show() methods of the FloatingActionButton.

BottomAppBar FabCradleMargin becoming less, almost flat, when BottomAppBar returns from being hidden

I am having an issue with my FabCradleMargin becoming less, almost flat, inside my Bottom App Bar when navigating through my app and scrolling up/down while hideonScroll is set to true. When the BottomAppBar hides from the screen, it returns resized under the Floating action Button. Must be a glitch in the new Android Material Components. Has anyone else been experiencing this issue. If so, what suggestions do you have to fixing it.
and
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
app:elevation="4dp"
app:fabAlignmentMode="center"
app:fabCradleRoundedCornerRadius="2dp"
app:hideOnScroll="true"
app:layout_scrollFlags="scroll|enterAlways"
app:navigationIcon="#drawable/ic_action_list" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/blue500"
app:fabSize="normal"
app:layout_anchor="#+id/bar"
app:tint="#color/white"
app:layout_anchorGravity="right"
app:srcCompat="#drawable/ic_select_camera" />
I stumbled upon this problem as well. In my case it depended on the way I tried to hide the BottomAppBar and the FloatingActionButton. This is what I had first (Kotlin):
private fun showBottomNavigationBar(barVisibility: Boolean, fabVisibility: Boolean) {
navView.visibility = if (barVisibility) BottomAppBar.VISIBLE else BottomAppBar.GONE
fab.visibility = if (fabVisibility) FloatingActionButton.VISIBLE else FloatingActionButton.GONE
}
And this is what fixed it:
private fun showBottomNavigationBar(barVisibility: Boolean, fabVisibility: Boolean) {
navView.visibility = if (barVisibility) BottomAppBar.VISIBLE else BottomAppBar.GONE
if (fabVisibility) fab.show() else fab.hide()
}
So instead of hiding the FloatingActionButton with the visibility property, I used the hide() and show() methods of the FloatingActionButton.

Bottom Navigation with center button arc or hump [duplicate]

This question already has answers here:
New Bottom Layout from Google
(3 answers)
Closed 4 years ago.
Looking to modify the Bottom Navigation Bar with a main center action button which changes the shape of the navigation bar itself either an arc like so
or a hump in the bottom nav enveloping the center button around it.
I'm fine if I have to extend the BottomNavigationView but I don't know where to start.
I found this similar question, but it's not the exact problem I'm trying to solve.
Bro, try this
Just place the BottomAppBar and FloatingActionButton into parent CoordinatorLayout and set app:layout_anchor attribute of FloatingActionButton to reference id of BottomAppBar.
<android.support.design.bottomappbar.BottomAppBar
android:id="#+id/bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:fabAttached="true"
app:backgroundTint="#color/colorPrimary"
app:fabCradleVerticalOffset="12dp">
</android.support.design.bottomappbar.BottomAppBar>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:src="#drawable/ic_add_white_24dp"
app:layout_anchor="#+id/bottom_appbar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.button.MaterialButton
android:id="#+id/toggle_alignment"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center"
android:textColor="#fff"
android:text="Toggle FAB alignment"
app:backgroundTint="#color/colorPrimary"/>
</FrameLayout>
To set menu options you can provide Menu resource to your BottomAppBar in code by calling BottomAppBar.replaceMenu(R.menu.xxx) and to toggle the alignment I created a simple extensions function
import kotlinx...bottom_appbar
import kotlinx...fab
import kotlinx...toggle_alignment
// using kotlin-android-extensions here for findViewById calls
class BottomAppBarActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.actvity_bottom_appbar)
//setSupportActionBar(bottom_appbar) calling this breaks it!
// setting the menu
bottom_appbar.replaceMenu(R.menu.bottom_appbar_menu)
toggle_alignment.setOnClickListener {
bottom_appbar.toggleAlignment()
}
}
fun BottomAppBar.toggleAlignment() {
val current = fabAlignmentMode
fabAlignmentMode = current.xor(1)
}
}
This type of Bottom App Bar comes with Support Library v28. This article by Joe Birch will be helpful.

ToolBar disappears when setting elevation for AppBarLayout

My ToolBar disappears when setting elevation for AppBarLayout. Here's the layout.
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="#dimen/appbar_height"
app:elevation="0dp"
android:background="#color/transparent">
<android.support.v7.widget.Toolbar
style="#style/ToolBarStyle"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:background="#drawable/backgorund_toolbar_tranluscent"
android:minHeight="#dimen/abc_action_bar_default_height_material" />
</android.support.design.widget.AppBarLayout>
I have tried values like 0dp, 0.1dp and 4dp for app:elevation. What's happening here? Is it a support library bug? I'm using 24.0.0.
Answer from #Zeeshan is totally right.
as an extra here is a sample code that works
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
StateListAnimator stateListAnimator = new StateListAnimator();
stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(appBarLayout, "elevation", 0.1f));
appBarLayout.setStateListAnimator(stateListAnimator);
}
I had to set elevation to 0.1 because setting it to 0 wasn't working, the whole Layout was disappearing.
New Update: In Appcompat v24.0.0, you can not set elevation to AppBarLayout using setElevation() and app:elevation as these are deprecated.
You have to use stateListAnimator property to set elevation now.
Note: set duration to 1ms in StateListAnimator in order to avoid delay in Elevation Drawing.
AppBarLayout elevation change is delayed on appCompat v24.0.0

FloatingActionButton setVisibility() not working

I can't hide my FloatingActionButton. Here is my code:
XML:
<CoordinatorLayout>
<AppBarLayout android:id="#+id/appbar">
<CollapsingToolbarLayout>
<ImageView/>
<android.support.v7.widget.Toolbar />
</CollapsingToolbarLayout>
</AppBarLayout>
<NestedScrollView />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end"/>
</CoordinatorLayout>
And I'm calling:
fab.clearAnimation();
fab.setVisibility(View.GONE);
I'm trying to hide the FAB, but it seems that setVisibility + clearAnimation does not work if the FAB is in a CoordinatorLayout.
Even if I call fab.clearAnimation, the animation is still triggered. Can anyone help me?
Use show and hide methods to show and hide the Floating Action Button
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
// To show the Floating Action Button
fab.show();
// To hide the Floating Action Button
fab.hide();
For FloatingActionButton the setVisibility() method will give you an error while building with the latest Gradle 6.x and build-tool 28.x.x and is not encouraged any more. Instead, use:
fab.hide() // fab.setVisibility(View.GONE)
fab.show() // fab.setVisibility(View.VISIBLE)
Note: Succesfully tested on Android Studio 3.4.1, Gradle 6.0 and
build-tool 28.0.1
If your issue is the animation, you could try invalidating the FAB Behavior. As for the visibility, you should null the anchor you have set in your layout:
CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setBehavior(null); //should disable default animations
p.setAnchorId(View.NO_ID); //should let you set visibility
fab.setLayoutParams(p);
fab.setVisibility(View.GONE); // View.INVISIBLE might also be worth trying
//to bring things back to normal state
CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setBehavior(new FloatingActionButton.Behavior());
p.setAnchorId(R.id.appbar);
fab.setLayoutParams(p);
I ran into exactly the same issue. It would seem that Google's Android team doesn't want you to have control of the visibility when the FloatingActionButton is anchored to an AppBarLayout, as discussed in this issue - FloatingActionButton Ignores View Visibility
It looks like a viable fix is wrapping the FAB in a FrameLayout and setting the visibility on the wrapper instead, like this:
<android.support.design.widget.FrameLayout
android:id="#+id/fab_container"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="invisible">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fab_icon"/>
</android.support.design.widget.FrameLayout>
You may however wish to consider whether this is the ideal behaviour. Google advocates recommend that the FAB be visible as soon as the screen is created. If you're hiding it for longer than required to animate it in, consider showing a disabled state instead.
you can disable it and make it semitransparent like this
fab.setEnabled(false);
fab.setClickable(false);
fab.setAlpha(0.3f);
FloatingActionButton layers = (FloatingActionButton) findViewById(R.id.layers);
layers.hide();
It works for me, setVisibility doesn't work for FloatingActionButton as it belongs to another viewGroup that doesn't have setVisibility method.

Categories

Resources