Add Material Design Floating Action Button(FAB) in Android Studio app project with support library
How to create android app with FAB and implement setOnClickListener() function on floating action button.
Floating action button is a new type of widget element extend with ImageButton. It is basically used for a special promoted action and displays the right bottom side of android UI screen. So in this tutorial we are adding floating action button inside our current android activity by using Design support library and after adding it we are going to add click event on it. So here is the complete step by step tutorial for Add Material Design Floating Action Button(FAB) in android studio app project with support library.
Note : Please follow the below steps very carefully in order to implement SnackBar in your android application :
1. Open your project’s build.gradle ( Module : app ) file.
2. Please add below code inside your build.gradle ( Module : app ) file.
compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:design:23.2.+'
3. Screenshot of build.gradle ( Module : app ) file after adding above code.
Here your go friends….Now We are going start coding.
How to Add Material Design Floating Action Button(FAB) in android studio app project with support library.
Code for MainActivity.java file.
package com.android_examples.floatingactionbuttonfab_android_examplescom; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } }
Code for activity_main.xml layout file.
<?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.android_examples.floatingactionbuttonfab_android_examplescom.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> </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="16dp" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>
Code for context_main.xml layout file.
<?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.android_examples.floatingactionbuttonfab_android_examplescom.MainActivity" tools:showIn="@layout/activity_main"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Floating Action Button" /> </RelativeLayout>
Code for styles.xml file.
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> </resources>
Comments
Post a Comment