FAB not moving down on Snackbar dismiss - android

On my phone and that of a friend, the FAB in the default Android Studio Activity does not move down after the Snackbar dismisses.
On the emulator and a few other friends' phones, it does animate down.
The first friend and I have changed animation scale to 0.5, but setting it to 1.0 and rebooting hasn't fixed the issue.
This should have been fixed in Design Support Library 23.2.0, and I am using 23.3.0, as the example comes with that set.
Edit:
The layout and code for completion purposes:
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.bas.test.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.example.bas.test.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_folder_open_black_24dp" />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
View.OnClickListener mOnClickListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
if (fab != null) {
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "You clicked the FAB!", Snackbar.LENGTH_LONG)
.setAction("Change bar text", mOnClickListener).show();
}
});
}
mOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
getSupportActionBar().setTitle("Changed!");
}
};
}
}

As suggested by #ianhanniballake, this was fixed in Design Support Library 23.4.0.

Related

Toolbar in non-AppCompat project not inflating

I tried to implement my toolbar with my activity but it's not inflating and keeps showing an error. My project does not use AppCompat but I'm not sure whether or not that is also the vause of the error.
Error inflating class android.widget.Toolbar
Java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar customToolbar = findViewById(R.id.toolbar_1line);
setActionBar(customToolbar);
//add back arrow to toolbar
if (getActionBar() != null){
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(true);
}
TextView mTitle = this.findViewById(R.id.toolbar_title);
mTitle.setText(getString(R.string.select_a_destination_station));
mTitle.setTextColor(Color.WHITE);
mTitle.setEllipsize(TextUtils.TruncateAt.MARQUEE);
mTitle.setMarqueeRepeatLimit(-1);
mTitle.setSingleLine(true);
mTitle.setSelected(true);
}
}
toolbar layout
<?xml version="1.0" encoding="utf-8"?>
<android.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_1line"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize">
<LinearLayout
android:id="#+id/singleline_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Material.Widget.ActionBar.Title"/>
</LinearLayout>
</android.widget.Toolbar>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/detail_container">
<include layout="#layout/toolbar_singleline"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/list_objects"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Try this.
In the Toolbar_layout
One way
change
<android.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_1line"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize">
</android.widget.Toolbar>
to
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_1line"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize">
</android.support.v7.widget.Toolbar>
In the java code
change
Toolbar customToolbar = findViewById(R.id.toolbar_1line);
setActionBar(customToolbar);
//add back arrow to toolbar
if (getActionBar() != null){
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(true);
}
to
Toolbar customToolbar = (Toolbar) findViewById(R.id.toolbar_1line);
setSupportActionBar(customToolbar);
//add back arrow to toolbar
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
Another way
change
<android.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_1line"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:minHeight="?android:attr/actionBarSize">
</android.widget.Toolbar>
to
<Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar_1line"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize">
</Toolbar>
It's easy.

MutableLiveData's observer is triggered before layout has been inflated

I have a MainActivity that contains a NavigationView for the drawer menu. The NavigationView's header contains a TextView that I want to populate with some user information.
The problem is, that sometimes the app crashes in the onChanged method with a NullPointerException because textUsername is null. My guess is that's because the layout has not been fully inflated yet. I have implemented the null check (as seen in the code below), which obviously just results in the textUsername being blank in most cases.
My onCreate method looks like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
mainDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
mainNavigationView = (NavigationView) findViewById(R.id.main_navigation_view);
mainContentLayout = (CoordinatorLayout) findViewById(R.id.main_content_layout);
// get user information
NetworkController.getInstance().getCurrentUser(Utils.getAuthenticationToken(), new NetworkController.GetUserResponseCallback() {
#Override
public void onGetUser(Response<UserResponse> response) {
if(response.isSuccessful() && response.body() != null && response.body().isSuccess()) {
mainViewModel.setCurrentUser(response.body().getUser());
}
}
#Override
public void onRequestFailed(Throwable throwable) {
}
});
mainViewModel.getCurrentUser().observe(this, new Observer<User>() {
#Override
public void onChanged(#Nullable User user) {
TextView textUsername = (TextView) mainNavigationView.findViewById(R.id.drawer_header_username);
if(textUsername != null) {
textUsername.setText(user.getProfile().getUsername());
}
}
});
}
How can I make sure that that layout is inflated before I access it from the callback? Thanks.
EDIT
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:id="#+id/main_drawer_layout">
<include
layout="#layout/activity_main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/main_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/drawer_menu_header"
app:menu="#menu/main_drawer_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
activity_main_content.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_content_layout">
<!-- setup the toolbar -->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="#dimen/toolbar_elevation"
android:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<!-- include the fragment container -->
<android.support.constraint.ConstraintLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:activity="com.fishley.android.activities.MainActivity"
android:id="#+id/main_fragment_container"
android:paddingTop="?attr/actionBarSize">
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_new_post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_plus"
android:tint="#android:color/white"
android:visibility="gone"/>
</android.support.design.widget.CoordinatorLayout>
drawer_menu_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:background="#drawable/drawer_menu_header_background">
<TextView
android:id="#+id/drawer_header_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textColor="#android:color/black"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="android.studio#android.com"/>
</LinearLayout>

Slide up panel like in stock Dialer on Nexus phones

In my app, when user clicks on a contact icon, I want to display contact's information in a slide, that appears from bottom and can be swiped up to cover the whole screen, or swiped down to dissapear.
I tried to use the SlidingUpPanel from umano, but I couldn't make it work as I need, because it was not designed for that.
Is there some library for that?
This is called a BottomSheet in Android. You can create your own bottom sheet simply using a view as a Child of a coordinator layout.
Add to your app build.gradle:
dependencies{
compile 'com.android.support:design:24.1.1'
}
And then use these classes to create your own:
BottomSheetDialogFragment
BottomSheetDialog
BottomSheetBehavior
BottomSheetBehavior.BottomSheetCallback
Here is an overly simplified demo to show how to make a FrameLayout act as a bottom sheet, you can replace the contents of the FrameLayout with a Fragment and pass information to it as needed. (in your case whichever contact was clicked):
The layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.sample.bottomsheetsample.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.kdotj.bottomsheetsample.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
<!-- Notice a few things:
app:layout_behavior="#string/bottom_sheet_behavior" required to act as bottom sheet
app:behavior_peekHeight="244dp" this is the collapsed state height
app:behavior_hideable="true" lets you swipe to dismiss the sheet
android:elevation="#dimen/design_appbar_elevation" puts the sheet over the action bar
-->
<FrameLayout
android:id="#+id/fl_bottomSheet"
app:layout_behavior="#string/bottom_sheet_behavior"
app:behavior_peekHeight="244dp"
app:behavior_hideable="true"
android:elevation="#dimen/design_appbar_elevation"
android:background="#777"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:layout_gravity="center|top"
android:text="Hello, Bottom Sheet!"
android:padding="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
the Activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.fl_bottomSheet);
final BottomSheetBehavior behavior = BottomSheetBehavior.from(frameLayout);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
//... Handle the state changes
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
//.. handle sliding if you want to
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
}
}

Toolbar on top of layout when using CoordinatorLayout, Swipe2Refresh and WebView

Im having issues with the toolbar being display ontop of the webview and i am trying to figure out how to remedy this. Any and all feedback is greatly appreciated.
webview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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" android:fitsSystemWindows="true"
tools:context="com.example.ui.activities.WebActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_web" />
</android.support.design.widget.CoordinatorLayout>
content_web.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/swipe_refresh">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webview" />
</android.support.v4.widget.SwipeRefreshLayout>
Here some code regarding how i am setting up the webview.
public class WebActivity extends BaseActivity {
#Bind(R.id.webview)
WebView webView;
#Bind(R.id.toolbar)
Toolbar toolbar;
#Bind(R.id.swipe_refresh)
public SwipeRefreshLayout swipeRefreshLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
setSupportActionBar(toolbar);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
}
});
if (savedInstanceState == null) {
webViewUrl = AUTH_URL;
runOnUiThread(new Runnable() {
#Override
public void run() {
webView = initWebView();
webView.loadUrl(webViewUrl);
}
});
} else {
webViewUrl = savedInstanceState.getString(KEY_URL,AUTH_URL);
runOnUiThread(new Runnable() {
#Override
public void run() {
webView.loadUrl(webViewUrl);
}
});
}
}
}
Any and all help will be very appreciated.
Layouts in a CoordinatorLayout need to define a layout_behavior. Change your content to this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/swipe_refresh"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webview" />
</android.support.v4.widget.SwipeRefreshLayout>

Floating Action Button on top of Snackbar. How?

I'm trying to show my FloatingActionButton on top of the SnackBar but I just can't make it as I have done on other screens. My current layout is this one:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="#dimen/toolbar_elevation"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.ToolbarPopUp"
app:theme="#style/AppTheme.Toolbar"/>
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
style="#style/Feed.TabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"/>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator_fab"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
style="#style/FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="#drawable/ic_start"
app:backgroundTint="#color/h19_green"
app:elevation="6dp"
app:pressedTranslationZ="12dp"/>
</android.support.design.widget.CoordinatorLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/navdrawer"/>
</android.support.v4.widget.DrawerLayout>
And my Snackbar code is this one:
#Override
public void alertNoInternetConnection() {
CoordinatorLayout coordinatorLayout = ButterKnife.findById(getActivity(), R.id.coordinator_fab);
Snackbar.make(coordinatorLayout, R.string.connection_offline, Snackbar.LENGTH_INDEFINITE).show();
}
How should I reorganize my layout so that when I display the Snackbar, the Floating Action Button hovers above it?
I was looking quite long for the answer, and finally I've found the solution which worked for me:
FloatingActionButton must be inside CoordinatorLayout
Snackbar should receive FloatingActionButton as it's View argument
Layout:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="#mipmap/ic_create_white_48dp"
/>
</android.support.design.widget.CoordinatorLayout>
Code:
FloatingActionButton fabAdd = (FloatingActionButton)findViewById(R.id.fabAdd);
Snackbar.make(fabAdd, "Record was removed.", Snackbar.LENGTH_SHORT).show();
Try adding this to your FAB:
app:layout_anchor="#id/coordinator_fab"
app:layout_anchorGravity="bottom|right"
and remove:
android:layout_gravity="bottom|end"
Your layout implementation is wrong, here's the right implementation (for example):
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
... >
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_done" />
</android.support.design.widget.CoordinatorLayout>
<include layout="#layout/navdrawer"/>
</android.support.v4.widget.DrawerLayout>
And then, to show the Snackbar correctly:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
The problem is that according to the google guidelines you should not display the FAB above the Snackbar you should swipe the FAB and display the Snack bar below it
check this link :
http://www.google.com/design/spec/components/snackbars-toasts.html#snackbars-toasts-usage
this worked for me.
<android.support.design.widget.CoordinatorLayout
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.FloatingActionButton
... />
fabBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Snackbar.make(rootLayout, "Hello. I am Snackbar!",
Snackbar.LENGTH_SHORT)
.setAction("Undo", new View.OnClickListener() {
#Override
public void onClick(View v) {
}
})
.show();
}
});

Categories

Resources