Floating action button
Floating action button (FAB) is simply a circle button with rounded shadow, floats above the UI and used to display any promoted action.
Floating action button can be an either of two sizes :
- normal (56dp)
- mini (40dp)
To use Floating action button you need to do following steps :
1. Add design support library in gradle file.
compile
'com.android.support:design:22.2.0'
2. Include FloatingActionButton in xml layout. you need to include xmlns:app=”http://schemas.android.com/apk/res-auto as namespace the top of your layout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?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:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.design.widget.FloatingActionButton android:id="@+id/btnFab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:src="@android:drawable/ic_delete" app:fabSize="normal" app:rippleColor="@android:color/background_dark" /> </android.support.design.widget.CoordinatorLayout> |
3. Write your code in JAVA file.
1 2 3 4 5 6 7 |
FloatingActionButton btnFab = (FloatingActionButton) findViewById(R.id.btnFab); btnFab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Delete item", Toast.LENGTH_SHORT).show(); } }); |
4. Output
Lolipop FloatingActionButton
Kitkat FloatingActionButton
Floating Action Button Between Two Layouts
You need to take parent layout as CoordinatorLayout.
in your xml :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?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:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/relative1" android:layout_width="match_parent" android:layout_height="300dp" android:background="@android:color/darker_gray"></RelativeLayout> <RelativeLayout android:id="@+id/relative2" android:layout_width="match_parent" android:layout_height="300dp" android:layout_below="@+id/relative1" android:background="@android:color/black"></RelativeLayout> </RelativeLayout> <android.support.design.widget.FloatingActionButton android:id="@+id/btnFab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:clickable="true" android:src="@android:drawable/ic_delete" app:borderWidth="0dp" app:layout_anchor="@id/relative1" app:layout_anchorGravity="bottom|right|end" /> </android.support.design.widget.CoordinatorLayout> |
Output :
Ravi Rupareliya
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019