I am working on Snack bar and Floating Action button. I used Coordinator layout for making the Floating action button to appear/move when snackbar is displayed. The problem is i kept an action for snackbar . When the floating button is tapped , Snackbar is popping up and Floating action button is moving up. And when i pressed the snackbar action item , the floating action button is getting hidden under the child snackbar.
And also if i press floating action button consecutively , then also floating action button is getting hidden.
Following is my code.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dev.firsttest.Screen2"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary_color"></android.support.v7.widget.Toolbar>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinatorlayout">
<android.support.design.widget.FloatingActionButton
android:id="@+id/searchfab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="@drawable/ic_add_black_24dp"
app:fabSize="normal">
</android.support.design.widget.FloatingActionButton>
</android.support.design.widget.CoordinatorLayout>
MainActivity
Toolbar toolbar;
FloatingActionButton searchfab;
CoordinatorLayout coordinatorLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen2);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
coordinatorLayout = (CoordinatorLayout)findViewById(R.id.coordinatorlayout);
searchfab = (FloatingActionButton)findViewById(R.id.searchfab);
searchfab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(coordinatorLayout, "This is Snackbar Demo", Snackbar.LENGTH_LONG).setAction("Click", new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(coordinatorLayout, "This is Child Snackbar", Snackbar.LENGTH_LONG).show();
}
}).show();
}
});
}
Pressing Child action in Snackbar and consecutive taps on Floating action button makes the Floating action button hides back to the Snackbar
Appreciate your help
Thank you
To show and hide a FloatingActionButton with the default animation, just call the methods show() and hide() . It's good practice to keep a FloatingActionButton in the Activity layout instead of putting it in a Fragment, this allows the default animations to work when showing and hiding.
A floating action button (FAB) is a circular button that triggers the primary action in your app's UI. This page shows you how to add the FAB to your layout, customize some of its appearance, and respond to button taps.
The button should be placed in the bottom right corner of the screen. The recommended margin for the bottom is 16dp for phones and 24dp for tablets. In the example above, 16dp was used. The actual drawable size should be 24dp according to the Google design specs.
The answer its here: https://github.com/ggajews/coordinatorlayoutwithfabdemo .
It will move the FAB when the snackbar is shown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With