Xamarin Android: Create Android Card Flip Animation Using Gesture Event - Part One

Let’s start,

Step 1: Open Visual Studio->New Project->Templates->Visual C#->Android->Blank App

Select blank app. Give the Project Name and Project Location.

Step 2: Next to Create Card_Front.axml page, go to Solution Explorer-> Project Name->Resources->Layout. Right click to Add-> New Item, open new Dialog box.

Step 3: This dialog box is required to select Android Layout and give the Name for Card_Front.axml.



Step 4:
Open Solution Explorer-> Project Name-> Resources-> Layout-> Main.axml. Click to open Design View. Give the following code:


XAML Code

  1. <?xml version="1.0" encoding="utf-8"?>    
  2.    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.       android:orientation="vertical"    
  4.       android:layout_width="match_parent"    
  5.       android:layout_height="match_parent"    
  6.       android:minWidth="25px"    
  7.       android:minHeight="25px">    
  8.    <FrameLayout    
  9.       android:minWidth="25px"    
  10.       android:minHeight="25px"    
  11.       android:layout_width="match_parent"    
  12.       android:layout_height="match_parent"    
  13.       android:id="@+id/frameLayout1" />    
  14.    </LinearLayout>  

Step 5: Open Solution Explorer-> Project Name-> Resources-> layout->Card_Front.axml. Click to open Design View.

Step 6: Select toolbar, drag and drop ImageView Design.


AXML Code

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.       android:orientation="vertical"    
  4.       android:layout_width="match_parent"   
  5.       android:layout_height="match_parent"   
  6.       android:minWidth="25px"    
  7.       android:minHeight="25px">    
  8.    <ImageView    
  9.       android:src="@drawable/rose"    
  10.       android:layout_width="match_parent"    
  11.       android:layout_height="wrap_content"   
  12.       android:id="@+id/imageView1"    
  13.       android:scaleType="center"    
  14.       android:contentDescription="Image" />  
  15. </LinearLayout>  

Step 7: Open MainActivity.cs page, create Gesture variable.

GestureDetector gesturedetector;

Step 8: Within the Oncreate(), declare gesture event and Fragment transaction.

 

C# Code
  1. gesturedetector = new GestureDetector(thisnew MyGestureListener(this));  
  2. if (bundle == null)  
  3. {  
  4.     FragmentTransaction trans = FragmentManager.BeginTransaction();  
  5.     trans.Add(Resource.Id.frameLayout1, new CardFrontFragment());  
  6.     trans.Commit();  
  7. }  

Step 9: After Oncreate(), create new class CardFrontFragment.


C# Code

  1. public class CardFrontFragment: Fragment  
  2. {  
  3.     public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
  4.     {  
  5.         View frontcard_view = inflater.Inflate(Resource.Layout.card_front, container, false);  
  6.         frontcard_view.Touch += Frontcard_view_Touch;  
  7.         return frontcard_view;  
  8.     }  
  9.     private void Frontcard_view_Touch(object sender, View.TouchEventArgs e) {  
  10.         MainActivity myactivity = Activity as MainActivity;  
  11.         myactivity.gesturedetector.OnTouchEvent(e.Event);  
  12.     }  
  13. }  

Step 10: Below CardFrontFragment.class, create one more new class for MyGestureListener. Here, we define the gesture by click event, DoubleClick(), scroll(), LongPress(), OnFling().

C# Code

  1. public class MyGestureListener: GestureDetector.SimpleOnGestureListener {  
  2.     public override bool OnDoubleTap(MotionEvent e) {  
  3.         Console.WriteLine("Double Tab");  
  4.         return true;  
  5.     }  
  6.     public override void OnLongPress(MotionEvent e) {  
  7.         Console.WriteLine("Long Press");  
  8.     }  
  9.     public override bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
  10.         Console.WriteLine("Fling");  
  11.         return base.OnFling(e1, e2, velocityX, velocityY);  
  12.     }  
  13.     public override bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
  14.         Console.WriteLine("Scroll");  
  15.         return base.OnScroll(e1, e2, distanceX, distanceY);  
  16.     }  
  17. }  

Step 11: Debug and run the app.

 

Finally, we successfully created Xamarin Android Gesture event in this part. We will see in the next part how to create animations between the two fragments.

Next Recommended Readings