startupsmith
startupsmith

Reputation: 5764

MonoDroid how to use custom attributes inside a layout axml file?

I am trying to convert the DragnDrop listview from here https://github.com/commonsguy/cwac-touchlist to C# with Mono for Android.

Part of this custom View requires the use of some custom attributes that are declared as the following inside the file Resources/values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TouchListView">
    <attr name="normal_height" format="dimension" />
    <attr name="expanded_height" format="dimension" />
    <attr name="grabber" format="reference" />
    <attr name="dragndrop_background" format="color" />
    <attr name="remove_mode">
        <enum name="none" value="-1" />
        <enum name="fling" value="0" />
        <enum name="slide" value="1" />
        <enum name="slideRight" value="1" />
        <enum name="slideLeft" value="2" />
    </attr>
</declare-styleable>
</resources>

I then try to use them inside my layout file like this:

<app.monodroid.TouchListView xmlns:tlv="http://schemas.android.com/apk/res/app.monodroid"       
    android:id="@+id/lstExercises"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/lExerciseActions"
    android:drawSelectorOnTop="false"
    tlv:normal_height="64dip"
    tlv:grabber="@+id/icon"
    tlv:remove_mode="slideRight"
    />

But when I try to build my project I get the following error message:

/Library/Frameworks/Mono.framework/External/xbuild/Novell/Novell.MonoDroid.Common.targets: Error: Tool exited with code: 1. Output: /Users/path_to_project/App.MonoDroid/obj/Debug/res/layout/add_session.axml:1: error: No resource identifier found for attribute 'normal_height' in package 'com.app.monodroid' /Users/path_to_project/App.MonoDroid/obj/Debug/res/layout/add_session.axml:1: error: No resource identifier found for attribute 'grabber' in package 'com.app.monodroid' /Users/path_to_project/App.MonoDroid/obj/Debug/res/layout/add_session.axml:1: error: No resource identifier found for attribute 'remove_mode' in package 'com.app.monodroid' (App.MonoDroid)

The name of my project is App.MonoDroid.

How can I use these attributes inside my layout file?

Upvotes: 2

Views: 2542

Answers (2)

samus
samus

Reputation: 6202

... and then make sure you use this package name for your assembly and namespace:

monodroid config

and use it in your xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:custom="http://schemas.android.com/apk/res/**app.monodroid**"            
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <**app.monodroid**.FICalendarView
    android:id="@+id/FICalendar"
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"
    custom:dayStyle="1">
  </**app.monodroid**.FICalendarView>
  <Button  
    android:id="@+id/MyButton"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/Hello"/>
</LinearLayout>

where FICaldendarView is a custom view defined as:

namespace app.monodroid
{
    public class FICalendarView : View
    {
        //private FICalenderView() {}

        public FICalendarView(Context context) 
            : base(context)
        {

        }

        public FICalendarView(Context context, Android.Util.IAttributeSet attribute) 
            : base(context, attribute)
        {
            Android.Content.Res.TypedArray a = context.Theme.ObtainStyledAttributes(attribute, Resource.Styleable.FICalendarView, 0, 0);

            //Android.Util.TypedValue typedValue = null;

            int dayStyle = a.GetInteger(Resource.Styleable.FICalendarView_dayStyle,0);

        }

        public FICalendarView(Context context, Android.Util.IAttributeSet attribute, int x)
            : base(context, attribute,x)
        {

        }
    }
}

Upvotes: 0

Greg Shackles
Greg Shackles

Reputation: 10139

These errors should go away if you declare a package name for your app. In the project properties, go to the Android Manifest tab, and you'll see a text field for Package Name:

Setting the package name

Upvotes: 3

Related Questions