FloatingActionButton does not provide shapes
other than rounded
see this Request: allow to set the shape of FAB
Yet there are many ways other than this to do But one which I personally recommend is using customFloatingActionButton library by robertlevonyan
, It is easy to use and customizable.
Square Shape
Add the following line of code to your module(app) level Gradle file
implementation 'com.robertlevonyan.view:CustomFloatingActionButton:3.0.1'
Now add the following code to activity_main.xml
<com.robertlevonyan.views.customfloatingactionbutton.FloatingActionButton
android:id="@+id/custom_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end" />
You can customize it in XML too by adding app:fabType="square"
but I
prefer MainActivity.java
Then customize floatingactionbutton
in MainActivity.java
public class MainActivity extends AppCompatActivity {
private FloatingActionButton floatingActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatingActionButton = findViewById(R.id.custom_fab);
floatingActionButton.setFabType(FabType.FAB_TYPE_SQUARE); //set button type to square
floatingActionButton.setFabIcon(getResources().getDrawable(R.drawable.ic_baseline_add_btn_24, null));
}
}
Output
