1

I have created a custom compound view inside a library application and everything was OK. When I add custom attributes to view, I always get default values. I followed this steps with only one difference: my view is in a library project.

/res/values/attrs.xml

<resources>
    <declare-styleable name="DatePickerView">
        <attr name="showToday" format="boolean" />
        <attr name="calendar" format="enum">
            <enum name="jalali" value="0" />
            <enum name="gregorian" value="1" />
        </attr>
    </declare-styleable>
</resources>

Layout file that contains view:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:farayan="http://schemas.android.com/apk/lib/net.farayan.android.view"
...
    <net.farayan.android.view.datepicker.DatePickerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        farayan:showToday="false"
        farayan:calendar="gregorian"/>
...

Component's code:

int calendar;
boolean showToday;

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DatePickerView, 0, 0);
try {
    calendar = a.getInteger(R.styleable.DatePickerView_calendar, 0);
    showToday = a.getBoolean(R.styleable.DatePickerView_showToday, true);
} finally {
    a.recycle();
}

calendar and showToday are always 0 and true respectively. Any idea?

Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106

2 Answers2

0

It looks like something is not right here:

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:farayan="http://schemas.android.com/apk/lib/net.farayan.android.view"

What about change to:

        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:farayan="http://schemas.android.com/apk/res/net.farayan.android.view"

net.farayan.android.view is your app root namespace.

update1

xmlns:farayan="http://schemas.android.com/apk/res/your_main_app_package"

Here is am example. It use a view defined in the library project.

srain
  • 8,944
  • 6
  • 30
  • 42
  • net.farayan.android.view is my root package declared in manifest file. – Ali Behzadian Nejad Jul 21 '13 at 09:58
  • As I told before, my view is defined in library application that is attached to the main app. attr.xml is also defined in library app. If I change `lib` to `res` while defining xml namespace, I get this error: `No resource identifier found for attribute 'calendar' in package` – Ali Behzadian Nejad Jul 21 '13 at 10:00
  • Is your code published anywhere? I appreciate your approach (just changing the layout of original calendar by changing YEAR, MONTH, DAY values to Jalali, so everything goes original beyond UI) – Xaqron Mar 10 '14 at 23:39
  • @AliBehzadianNejad, has your problem been solved? I was working on a library project and dealt the same issue. I updated the answer. Hope that would be helpful. – srain Mar 11 '14 at 03:24
  • @Xaqron I am doing a new version and it is not finished yet. When it finishes, I will publish it in my GitHub (https://github.com/alibehzadian/) – Ali Behzadian Nejad Sep 09 '14 at 05:13
0

If we add new compound view code and its attributesinside project, we should add this at the beginning of layout:

xmlns:custom="http://schemas.android.com/apk/res/your_main_app_package

and if new compound view is inside a library project linked to our peoject, we should add this:

xmlns:custom="http://schemas.android.com/apk/res-auto

Link: https://stackoverflow.com/a/10217752/1152549

Community
  • 1
  • 1
Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106