Question

I've made my custom view and i want to set some custom attributes to it. I want to pass another view's id as an attribute.

Custom View attrs:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="IdNumber">
        <attr name="firstName" format="integer"/>
        <attr name="lastName" format="integer"/>
        <attr name="address" format="integer"/>
        <attr name="birthDate" format="integer"/>
    </declare-styleable>
</resources>

Layout where i use my Custom View:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/display_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:layout_centerHorizontal="true"
        android:tag="name"
        android:ems="10" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/id_number"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/display_name"/>

    <ge.altasoft.custom_views.IdNumber
        android:id="@+id/custom_id_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/id_number"
        android:paddingLeft="35dip"
        custom:firstName="@id/display_name"/>


</RelativeLayout>

Custom View Class's constructor, where i want to get the attribute value:

 public IdNumber (Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IdNumber);
        final int N = a.getIndexCount();
        for(int i = 0; i < N; i++){
            int attr = a.getIndex(i);
            switch(attr){
                case R.styleable.IdNumber_firstName:
                    int firstNameViewID = a.getInteger(attr, -1);
                    break;
            }
        }
        a.recycle();
    }

The Problem is that int firstNameViewID = a.getInteger(attr, -1); is just 0, not the id of the View.

custom:firstName="@id/display_name" <<< here should be something wrong but I don't know what's wrong with it. When I assign to the custom attribute some Integer Value it works, but it doesn't work on Id-s.

Thanks for your help in Advance.

Was it helpful?

Solution

Change your define <attr name="firstName" format="reference"/> And in your code use int firstNameViewID = a.getResourceId(attr, -1);

Hope this help!

OTHER TIPS

use custom:firstName="@+id/display_name" and in your styleable change it to <attr name="firstName" format="refernce"/>instead of integer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top