anguish
anguish

Reputation: 458

Monodroid: using extended view class in xml

I have a Class that extends the HorizontalScrollView. But I don't know how I can use it in the xml. If I use my classname as xml tag, I got an error: Error inflating class MyClass

My Code:

public class ToothScroller : HorizontalScrollView
{

    private GestureDetector mGestureDetector;

    public ToothScroller(Context context) : base(context)
    {
    }

    public ToothScroller(Context context, IAttributeSet attrs) : base(context, attrs)
    {

        SetFadingEdgeLength(0);
    }

    public ToothScroller(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {
    }

    public ToothScroller(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }


    public override bool OnInterceptTouchEvent (Android.Views.MotionEvent ev)
    {
        Console.WriteLine(this.ScrollX);

        return base.OnInterceptTouchEvent (ev);
    }

}

And my xml (axml) code:

    <MyClass
        android:id="@+id/scrollMyClass"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/imageSomething"
        android:fillViewport="true"
        android:background="@drawable/pusherbg">
        <RelativeLayout
            android:id="@+id/layoutSomething"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </MyClass>

Thank you for help.

Upvotes: 1

Views: 605

Answers (3)

anguish
anguish

Reputation: 458

Thanks for help, it works now with the following code. Packagename was a little bit confusing, because in Monodroid, that I'm using, is it the namespace that I have to use, not the packagename.

<MyNamespace.MyClass
    android:id="@+id/scrollMyClass"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imageSomething"
    android:fillViewport="true"
    android:background="@drawable/pusherbg">
    <RelativeLayout
        android:id="@+id/layoutSomething"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</MyNamespace.MyClass>

Upvotes: 0

Rob Avery IV
Rob Avery IV

Reputation: 3740

Change your layout xml to this:

<com.username.package.MyClass
    android:id="@+id/scrollMyClass"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imageSomething"
    android:fillViewport="true"
    android:background="@drawable/pusherbg">
    <RelativeLayout
        android:id="@+id/layoutSomething"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</com.username.package.MyClass>

Here is a good resource with more detail straight from Google which probably is going to have a lot more information about how to create a custom view: http://developer.android.com/guide/topics/ui/custom-components.html

Upvotes: 1

Nirav Tukadiya
Nirav Tukadiya

Reputation: 3417

Replace this

 <MyClass
    android:id="@+id/scrollMyClass"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imageSomething"
    android:fillViewport="true"
    android:background="@drawable/pusherbg"> 

with

  <YourPackage.YourClass
    android:id="@+id/scrollMyClass"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imageSomething"
    android:fillViewport="true"
    android:background="@drawable/pusherbg">

Upvotes: 2

Related Questions