Skip to content

Instantly share code, notes, and snippets.

@vamsee9
Created August 19, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vamsee9/2c1227f0e42d6d2c4d151f1de71afe5b to your computer and use it in GitHub Desktop.
Save vamsee9/2c1227f0e42d6d2c4d151f1de71afe5b to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//in this you have to declare the functions
private static final String TAG = "MainActivity" ;
FloatingActionButton FabMainAdd, FabCamera, FabImportImage;
Float translationY = 100f;
OvershootInterpolator interpolator = new OvershootInterpolator();
Boolean isMenuOpen = false;
//**End of Declaritions
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init FAB menu
iniFabMenu();
}
private void iniFabMenu() {
//in this you need to refer FAB to xml icons
FabMainAdd = findViewById(R.id.main_add_fab);
FabCamera = findViewById(R.id.camera_fab);
FabImportImage = findViewById(R.id.import_image_fab);
//in this the animation set Alpha to 0 float point as it should not appear in app start-up
FabCamera.setAlpha(0f);
FabImportImage.setAlpha(0f);
//in this icons float down to-wards & out-wards main menu
FabCamera.setTranslationY(translationY);
FabImportImage.setTranslationY(translationY);
//In this step adding on click listeners to each FAB
FabMainAdd.setOnClickListener(this);
FabCamera.setOnClickListener(this);
FabImportImage.setOnClickListener(this);
}
private void openMenu() {
isMenuOpen = !isMenuOpen;
//in this step we added out-wards animation for all FABs
FabMainAdd.animate().setInterpolator(interpolator).rotation(45f).setDuration(300).start();
FabCamera.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();
FabImportImage.animate().translationY(0f).alpha(1f).setInterpolator(interpolator).setDuration(300).start();
}
private void closeMenu() {
isMenuOpen = !isMenuOpen;
//in this step we added in-wards animation for all FABs
FabMainAdd.animate().setInterpolator(interpolator).rotation(0f).setDuration(300).start();
FabCamera.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();
FabImportImage.animate().translationY(translationY).alpha(0f).setInterpolator(interpolator).setDuration(300).start();
}
//This is used to know the process of button Handler using Debug-Log
private void handleCameraFab() {
Log.i(TAG, "handleCameraFab: ");
}
@Override
public void onClick(View view) {
//In this step we added close and open menu for all floating buttons and also added Toast, Debug-Logs
//using switch-case statements
switch (view.getId()) {
case R.id.main_add_fab:
Log.i(TAG, "Select one of the menu");
if (isMenuOpen) {
closeMenu();
} else {
openMenu();
Toast.makeText(getApplicationContext(),"Select one of the menu",Toast.LENGTH_SHORT).show();
}
break;
case R.id.camera_fab:
Log.i(TAG, "Opening Camera.");
handleCameraFab();
if (isMenuOpen) {
closeMenu();
} else {
openMenu();
}
Toast.makeText(getApplicationContext(),"Opening Camera.",Toast.LENGTH_SHORT).show();
break;
case R.id.import_image_fab:
Log.i(TAG, "Opening Gallery..");
handleCameraFab();
if (isMenuOpen) {
closeMenu();
} else {
openMenu();
}
Toast.makeText(getApplicationContext(),"Opening Gallery.",Toast.LENGTH_SHORT).show();
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment