Skip to content Skip to sidebar Skip to footer

How To Pass View Reference To Android Custom View?

I did as follows 1) Creating a styleable 2) defining cust

Solution 1:

I found the answer!

The issue was with findViewById(id) and where I called it. findViewById only looks for a child view not a view exist on upper hierarchy level as documentation says . So I have to call something like getRootView().findViewById(id) but that also returns null becase where I called it was not corrent.

In Viewee constractor Viewee itself has not attached to its root yet so that call causes NullPointerException.

So If I call to getRootView().findViewById(id) somewhere else after constraction, it works fine and both "@+id/tvTest" and "@id/tvTest" are correct. I've tested it!

the answer is as follows

publicclassVieweeextendsLinearLayout
{
    publicViewee(Context context, AttributeSet a)
    {
        super(context, attributeSet);
        View.inflate(context, R.layout.main6, this);
        TextViewtextView= (TextView) findViewById(R.id.custom_text);
        TypedArrayt= context.obtainStyledAttributes(a, R.styleable.Viewee);
        intid= t.getResourceId(R.styleable.Viewee_linkedView, 0);
        if (id != 0)
        {
            _id = id;
        }

        t.recycle();
    }

    privateint _id;

    publicvoidFoo()
    {
        TextViewtextView= (TextView) findViewById(R.id.custom_text);
        Viewview= getRootView().findViewById(_id);
        textView.setText(((TextView) view).getText().toString());
    }
}

and Foo is called when it is required to process the attached view via its reference id somewhere else in your activity and the like.

The credit completely goes to those guys contributed in this post. I had not seen that post before submitting the question.

Solution 2:

I know this is an old question, but I thought I would add another way of doing this as I wanted to encapsulate everything into my custom view.

Instead of calling from the outside, another way of getting a view higher up in the hierarchy, I've hooked into onAttachedToWindow() instead:

publicclassMyCustomViewextendsLinearLayout {
    privateint siblingResourceId;
    private View siblingView;

    publicMyCustomView(Context context, AttributeSet a) {
        super(context, attributeSet);
        inflate(context, R.layout.main6, this);
        TextViewtextView= (TextView) findViewById(R.id.custom_text);
        TypedArrayt= context.obtainStyledAttributes(a, R.styleable.Viewee);
        siblingResourceId = t.getResourceId(R.styleable.MyCustomView_siblingResourceId, NO_ID);
        t.recycle();
    }

    @OverridepublicvoidonAttachedToWindow() {
        super.onAttachedToWindow();
        if (siblingResourceId != NO_ID) {
            siblingView = ((View) getParent()).findViewById(siblingResourceId);
        }
    }
}

onAttachedToWindow is called quite early, but apparently late enough for the whole view hierarchy to have settled. It works flawless for my needs at least and is a little bit more controlled and doesn't need interaction from outside to work ;-)

EDIT: Kotlin code added

classMyCustomView(context: Context, attributeSet: AttributeSet) : LinearLayout(context, attributeSet) {
    privateval siblingResourceId: Intprivatelateinitvar siblingView: View

    // All other constructors left out for brevity.init {
        inflate(context, R.layout.main6, this)
        val textView = findViewById<TextView>(R.id.custom_text)
        val t = context.obtainStyledAttributes(a, R.styleable.Viewee)
        siblingResourceId = t.getResourceId(R.styleable.MyCustomView_siblingResourceId, NO_ID)
        t.recycle()
    }

    overridefunonAttachedToWindow() {
        super.onAttachedToWindow()
        if (siblingResourceId != NO_ID) {
            siblingView = (parent as View).findViewById(siblingResourceId) 
        }
    }
}

Note: We assume that the parent of this custom View is a View itself.

Solution 3:

Your described android:id is set to app:linkedView="@+id/tvTest. However, @+id/tvTest is used to create a new id with name "tvTest". What you want to do is use app:linkedView="@id/tvTest.

Post a Comment for "How To Pass View Reference To Android Custom View?"