Android Floatingactionbutton Example

Posted on

FloatingActionButton are used for a particular type of promoted action. They are distinguished yesteryear a circled icon floating inwards a higher house the UI together with bring particular displace behaviors related to morphing, launching, together with the transferring anchor point.

Floating activeness buttons come upwards inwards ii sizes: the default together with the mini. The size tin live on controlled amongst the fabSize attribute.

Example:

<android.support.design.widget.CoordinatorLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_margin="20dp"     android:padding="10dp"     android:id="@+id/coordinatorLayout"     android:background="#000050"     tools:context=".MainActivity">      <RelativeLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_margin="20dp"         android:padding="10dp"         android:background="#005000">          <TextView             android:id="@+id/title"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:autoLink="web"             android:textSize="24dp"             android:layout_alignParentTop="true"             android:text="https://rootandroid.org//"             android:textStyle="bold" />          <TextView             android:id="@+id/textView"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textStyle="bold"             android:textSize="30dp"             android:layout_alignParentBottom="true"             android:text="Android FloatingActionButton example" />     </RelativeLayout>      <android.support.design.widget.FloatingActionButton         android:id="@+id/floatingActionButton1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginBottom="20dp"         android:layout_marginRight="20dp"         android:src="@mipmap/ic_launcher"         app:fabSize="normal"         app:layout_anchor="@id/coordinatorLayout"         app:layout_anchorGravity="bottom|left|end"/>      <android.support.design.widget.FloatingActionButton         android:id="@+id/floatingActionButton2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginBottom="20dp"         android:layout_marginRight="20dp"         android:src="@mipmap/ic_launcher"         app:fabSize="mini"         app:layout_anchor="@id/coordinatorLayout"         app:layout_anchorGravity="bottom|right|end"/>  </android.support.design.widget.CoordinatorLayout>  

package com.example.eric.androidfloatingactionbutton;  import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast;  world shape MainActivity extends AppCompatActivity {      FloatingActionButton floatingActionButton1, floatingActionButton2;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         floatingActionButton1 =                 (FloatingActionButton)findViewById(R.id.floatingActionButton1);         floatingActionButton2 =                 (FloatingActionButton)findViewById(R.id.floatingActionButton2);          floatingActionButton1.setOnClickListener(new View.OnClickListener(){             @Override             world void onClick(View v) {                 Toast.makeText(MainActivity.this,                         "floatingActionButton1 (normal) clicked",                         Toast.LENGTH_LONG).show();             }         });          floatingActionButton2.setOnClickListener(new View.OnClickListener(){             @Override             world void onClick(View v) {                 Toast.makeText(MainActivity.this,                         "floatingActionButton2 (mini) clicked",                         Toast.LENGTH_LONG).show();             }         });     }  }  

Design Support Library is needed, advert to “Add Android Design Support Library to Android Studio Project“.

Next:
CoordinatorLayout + FloatingActionButton + Snackbar of Android Design Support Library