Welcome to the part two of our today's android project.
In the previous part, we create animation XML and drawable. Now we add fab code in the layout. (if you miss part one click here)
In my case my layout is activity_main.xml before doing this we need two colors in colors.xml.
Let's add this
Note: if you have any dought you can check full activity.xml code on GitHub.
Now time for java code.
In my case java file is MainActivty.java Add those variables-
If the user clicks the main fab button then we will show others button. That's is the logic. ok add main fab clickListener and use below code to show others button. After the code, I discuss code logic again Ok enough talking. Let's start-
Note: youcan recheck MainActivity.java on GitHub
Code analysis-
1.In layout file when we created out floating action button we set those button's visibility gone excepts main fab button. we check boolean state first, isopen is true or false
2. If the answer is true that means fab is open now. So we set here close fab functionality.
2.1. set close animation on two fabs that are you want's to close.
2.2. set those fab clickable false.
2.3 set anti clock rotate animation on mainFab.
2.4 now time to invisible that's fab.
2.5 at last assign isopen to false.
3.If the answer is false that means fab is close. So we set here close fab functionality.
3.1. now time to invisible others fab.
3.1. set open animation on two fabs that are not shown.
3.2. set those fab clickable true.
3.3 set clock rotate animation on mainFab.
3.4 set click listener on those fab
3.5 at last assign isopen to true.
Wow. now we fab three fabs in our layout but only one is showing at first. After clicking main fab other's button is open with animation. And those button is now working. And again click the main fab button, others two buttons is gone. So welcome to you all. you successfully created this project.
you can find the full project on Github. Fab
Thanks for reading this post and hope this code is working and now you can use fab with animation on your own project.
Happy coding.
In the previous part, we create animation XML and drawable. Now we add fab code in the layout. (if you miss part one click here)
In my case my layout is activity_main.xml before doing this we need two colors in colors.xml.
Let's add this
<color name="fab1">#e2e21d</color> <color name="fab2">#47e92e</color>Now add three fab button. I share this code one by one.
<android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="150dp" android:layout_marginEnd="16dp" android:layout_gravity="bottom|end" android:elevation="6dp" android:src="@drawable/ic_person_outline_black_24dp" app:fabSize="normal" android:visibility="gone" android:id="@+id/fab_people" app:backgroundTint="@color/fab2" app:pressedTranslationZ="12dp"/>next one
<android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="80dp" android:layout_marginEnd="16dp" android:layout_gravity="bottom|end" android:elevation="6dp" android:src="@drawable/ic_school_black_24dp" app:fabSize="normal" android:visibility="gone" android:id="@+id/fab_school" app:backgroundTint="@color/fab1" app:pressedTranslationZ="12dp"/>and the last one
<android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/fab_margin" android:layout_gravity="bottom|end" android:elevation="6dp" android:src="@drawable/ic_share_black_24dp" app:fabSize="normal" android:id="@+id/main_fab" app:pressedTranslationZ="12dp"/>
Note: if you have any dought you can check full activity.xml code on GitHub.
Now time for java code.
In my case java file is MainActivty.java Add those variables-
FloatingActionButton main,people,school; Animation fabOpen,fabClose,fabRotate,fabRotateAntiClockWise; //fab is open or close boolean isOpen = false;Now assign all fab button with findViewById()
main = (FloatingActionButton) findViewById(R.id.main_fab); people = (FloatingActionButton) findViewById(R.id.fab_people); school = (FloatingActionButton) findViewById(R.id.fab_school);Now time for load animation-
fabOpen = AnimationUtils.loadAnimation(this,R.anim.fab_open); fabClose = AnimationUtils.loadAnimation(this,R.anim.fab_close); fabRotate = AnimationUtils.loadAnimation(this,R.anim.rotate_clockwise); fabRotateAntiClockWise = AnimationUtils.loadAnimation(this,R.anim.rotate_anit_clockwise);
If the user clicks the main fab button then we will show others button. That's is the logic. ok add main fab clickListener and use below code to show others button. After the code, I discuss code logic again Ok enough talking. Let's start-
main.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("Click", "click"); if (isOpen){ people.setAnimation(fabClose); school.setAnimation(fabClose); main.setAnimation(fabRotateAntiClockWise); people.setClickable(false); school.setClickable(false); people.setVisibility(View.GONE); school.setVisibility(View.GONE); isOpen = false; } else { people.setVisibility(View.VISIBLE); school.setVisibility(View.VISIBLE); people.setAnimation(fabOpen); school.setAnimation(fabOpen); main.setAnimation(fabRotate); people.setClickable(true); school.setClickable(true); people.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Welcome to people", Toast.LENGTH_SHORT).show(); } }); school.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Welcome to school", Toast.LENGTH_SHORT).show(); } }); isOpen = true; } } });
Note: youcan recheck MainActivity.java on GitHub
Code analysis-
1.In layout file when we created out floating action button we set those button's visibility gone excepts main fab button. we check boolean state first, isopen is true or false
2. If the answer is true that means fab is open now. So we set here close fab functionality.
2.1. set close animation on two fabs that are you want's to close.
2.2. set those fab clickable false.
2.3 set anti clock rotate animation on mainFab.
2.4 now time to invisible that's fab.
2.5 at last assign isopen to false.
3.If the answer is false that means fab is close. So we set here close fab functionality.
3.1. now time to invisible others fab.
3.1. set open animation on two fabs that are not shown.
3.2. set those fab clickable true.
3.3 set clock rotate animation on mainFab.
3.4 set click listener on those fab
3.5 at last assign isopen to true.
Wow. now we fab three fabs in our layout but only one is showing at first. After clicking main fab other's button is open with animation. And those button is now working. And again click the main fab button, others two buttons is gone. So welcome to you all. you successfully created this project.
you can find the full project on Github. Fab
Thanks for reading this post and hope this code is working and now you can use fab with animation on your own project.
Happy coding.