Skip to content

Instantly share code, notes, and snippets.

@smartherd
Created December 3, 2020 08:15
Show Gist options
  • Save smartherd/ac3df8275b7978a4ce53221dbe57f5b0 to your computer and use it in GitHub Desktop.
Save smartherd/ac3df8275b7978a4ce53221dbe57f5b0 to your computer and use it in GitHub Desktop.
Extended Floating Action Button Gist Files
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:background="#EDEEEF"
tools:context=".MainActivity">
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Linear layout to contain 2 text view and button -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- showing random text 1 from strings.xml file -->
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
<include layout="@layout/scroll_item" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/extendedFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_gravity="bottom|end"
android:text="Compose"
app:icon="@drawable/ic_edit"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
</vector>
package com.sriyank.mdccomponents
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.core.widget.NestedScrollView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nestedScrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
if (scrollY > oldScrollY + 20 && extendedFab.isExtended) { // Scroll Down
extendedFab.shrink()
}
if(scrollY < oldScrollY - 20 && !extendedFab.isExtended) { // Scroll Up
extendedFab.extend()
}
if (scrollY == 0) { // At the top
extendedFab.extend()
}
})
}
private fun showToast(str: String) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show()
}
}
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
// Scroll down
if (dy > 20 && extendedFab.isExtended) {
extendedFab.shrink()
}
// Scroll up
if (dy < -20 && !extendedFab.isExtended) {
extendedFab.extend()
}
// At the top
if (!recyclerView.canScrollVertically(-1)) {
extendedFab.extend()
}
}
})
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="2dp"
android:padding="20dp"
android:background="#EFEDED"
android:text="Sample Row Item"
android:textColor="@android:color/black"
android:textSize="18sp" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment