11

Is there any tutorial which will enable me to design custom views in xamarin?I want to build pinch zoom functionality for my android app using xamarin.

I have tried following code,but its not working,I am always getting android.view.InflateException: Binary XML file line #1: Error inflating class LA_Application.ZoomView error

using System;    
using System.Collections.Generic;
using System.Linq;    
using System.Text;    
using Android.App;    
using Android.Content;    
using Android.OS;    
using Android.Runtime;    
using Android.Util;    
using Android.Views;    
using Android.Widget;    
using Android.Graphics;

namespace LA_Application    
{

    public class ZoomView : FrameLayout      
    {

        private ScaleGestureDetector mScaleDetector;    
        private static float mScaleFactor = 1.0f;   


        public ZoomView (Context context) : base (context)
        {
            Initialize ();    
        }

        public ZoomView (Context context, IAttributeSet attrs) : base (context,attrs)    
        {
            Initialize ();    
        }

        public ZoomView (Context context, IAttributeSet attrs, int defStyle) : base (context, attrs, defStyle)
        {
            Initialize ();
        }

        void Initialize ()
        {
            mScaleDetector = new ScaleGestureDetector(Context, new ScaleListener());
        }

        public override bool OnTouchEvent (MotionEvent e)
        {
            mScaleDetector.OnTouchEvent(e);

            return true;
        }

        protected override void OnDraw(Android.Graphics.Canvas canvas)
        {
            base.OnDraw(canvas);

            canvas.Save();    
            canvas.Scale(mScaleFactor, mScaleFactor);
            canvas.Restore();
        }
    }

    private class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener
    {
        public override bool OnScale(ScaleGestureDetector detector)
        {
            mScaleFactor *= detector.ScaleFactor;

            // Don't let the object get too small or too large.
            mScaleFactor = Math.Max(0.1f, Math.Min(mScaleFactor, 5.0f));

            return true;
        }  
    }
}

}

and in layout file

<?xml version="1.0" encoding="utf-8"?>
<LA_Application.ZoomView xmlns:android="http://schemas.android.com/apk/res/android"
                         android:layout_width="match_parent"
                         android:layout_height="match_parent"
                         android:id="@+id/my_view" />

activity code

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    SetContentView(Resource.Layout.zoomview);

    /*some code*/
}
auhmaan
  • 726
  • 1
  • 8
  • 28
Cris
  • 12,799
  • 5
  • 35
  • 50

4 Answers4

7

In the layout file you need to write the path to your class in small letters. For me Core.Droid.MyImageView had to be written as core.droid.MyImageView.

I am not sure if only the first letter or all letter have to be written in small, but you can try either lA_Application.ZoomView or la_application.ZoomView. One of them will very likely work :)

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
Jens
  • 71
  • 1
  • 6
  • This fixed my issue. I was using the correct namespace in my project and it was working, but if the layout was in a sub library project, then I needed to use the lowercase version of the namespace ! – Stephane Mathis Aug 11 '14 at 08:49
2

Here is the skeleton of how to create a custom view in xamarin.android

http://xandroid4net.blogspot.com/2014/09/custom-view.html

Then take Emmanuel Touzery answer and add it to the skeleton

android pinch zoom

Community
  • 1
  • 1
honeyws
  • 151
  • 1
  • 3
1

I would suggest checking out this Java Android tutorial to get an idea of what you need to setup:

http://developer.android.com/training/custom-views/index.html

You may need to create an attributes xml file for your custom view.

Another approach you may want to consider is to use a fragment instead of a view:

http://docs.xamarin.com/guides/android/platform_features/fragments/fragments_walkthrough

Ben Bishop
  • 1,414
  • 9
  • 14
0

I'v met with a similar problem today, and I accidentally find a way to solve it. Just add a "Register" before your defination of class ZoomView .

namespace LA_Application    
{
    [Register("la_application.ZoomView")]
    public class ZoomView : FrameLayout      
    {
        ...
    }
}

Right click FrameLayout in Visual Studio, and click "go to defination", then you'll find a "Register" before it. Visual Studio tells me that it is the android runtime attribute. I guess this is the real problem.

By the way, don't forget to write your namespace in lowercase. Hope to help you somehow.

SMagic
  • 1
  • 2