Programmatically creating a view, complete with Android Binding attributes

3,100 views
Skip to first unread message

Giuseppe Piscopo

unread,
Jul 14, 2011, 5:42:19 AM7/14/11
to android...@googlegroups.com
Hello

I've always seen examples of Android Binding attributes (e.g. binding:onClick) applied to some XML node (e.g. <Button />) in some layout file.

Is it possible to create a layout programmatically (i.e. by code, not infalting it from an XML file), and still have some binding attribute applied to UI controls?

Say we take this piece of layout as an example:

    <Button
        android:text="A Button"
        binding:onClick="AButtonClick"/>

How can I create the same thing by means of just Java code, no XML?

Maybe it's just there somewhere and I am missing it completely.

Thanks for your attention
Giuseppe

Andriy Kulynyak

unread,
Jul 14, 2011, 7:00:04 AM7/14/11
to android...@googlegroups.com
Hi Giuseppe

It is just wired code-part for non nested template
I'm not sure (never tried)... but it can be something like this:

/******* CODE

AttributeSet attrX = new AttributeSet() {
// Don't forget add Binding attributes and  xmlns:binding="http://www.gueei.com/android-binding/"
            public int getAttributeCount() {
                return 0;
            }

            public String getAttributeName(int index) {
                return null;
            }

            public String getAttributeValue(int index) {
                return null;
            }

            public String getAttributeValue(String namespace, String name) {
                return null;
            }

            public String getPositionDescription() {
                return null;
            }

            public int getAttributeNameResource(int index) {
                return 0;
            }

            public int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue) {
                return 0;
            }

            public boolean getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue) {
                return false;
            }

            public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) {
                return 0;
            }

            public int getAttributeIntValue(String namespace, String attribute, int defaultValue) {
                return 0;
            }

            public int getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue) {
                return 0;
            }

            public float getAttributeFloatValue(String namespace, String attribute, float defaultValue) {
                return 0;
            }

            public int getAttributeListValue(int index, String[] options, int defaultValue) {
                return 0;
            }

            public boolean getAttributeBooleanValue(int index, boolean defaultValue) {
                return false;
            }

            public int getAttributeResourceValue(int index, int defaultValue) {
                return 0;
            }

            public int getAttributeIntValue(int index, int defaultValue) {
                return 0;
            }

            public int getAttributeUnsignedIntValue(int index, int defaultValue) {
                return 0;
            }

            public float getAttributeFloatValue(int index, float defaultValue) {
                return 0;
            }

            public String getIdAttribute() {
                return null;
            }

            public String getClassAttribute() {
                return null;
            }

            public int getIdAttributeResourceValue(int defaultValue) {
                return 0;
            }

            public int getStyleAttribute() {
                return 0;
            }
        };
               
        View x= new View(context, attrX);

/**  create corresponding model */
Binder.setAndBindContentView(x,someLayoutIdValueFromYour_attrX, model);

********/


Regards Andriy

2011/7/14 Giuseppe Piscopo <piscopo....@gmail.com>



--
З повагою,
Андрій Кулиняк
kuly...@gmail.com

Giuseppe Piscopo

unread,
Jul 14, 2011, 7:08:17 AM7/14/11
to android...@googlegroups.com
Uhm, not really clear to me, but of course thanks for your help.

I have no layout XML, as I want to build it programmatically. Hence, where in code should I "not forget to add binding attributes and xmlns..." ?

2011/7/14 Andriy Kulynyak <kuly...@gmail.com>

Andriy Kulynyak

unread,
Jul 14, 2011, 7:57:48 AM7/14/11
to android...@googlegroups.com
You have to implement  an AttributeSet to create View, so put binding stuff there.

14 липня 2011 р. 14:08 Giuseppe Piscopo <piscopo....@gmail.com> написав:

Andy Tsui

unread,
Jul 14, 2011, 9:56:21 AM7/14/11
to android...@googlegroups.com
Hi Giuseppe,

Although I never tried this, but I think this is possible:

Binding by default is handle by the static class "Binder", and in A-B, what I did is to place a mapping object (attrName-String) pair and in the second phase, bind with the mapping.

For what you wrote, you can:

Button btn = new Button(this);

// This will get the viewAttribute from the view, it will create if it is not yet created, and throw an exception if it is undefined.
ViewAttribute attr = Binder.getAttributeForView(btn, "onClick");

// here, you might need to cast it if you needed, but it's ok to write:
BindingType result = attr.BindTo(Observable);

the result will tell you whether the binding will be oneway, twoway or nothing, depending on types of your observable and the viewAttribute.

Hope this can help.

Andy

Andy Tsui

unread,
Jul 14, 2011, 9:57:54 AM7/14/11
to android...@googlegroups.com

Giuseppe Piscopo

unread,
Jul 14, 2011, 1:04:53 PM7/14/11
to android...@googlegroups.com
thanks Andriy & Andy for your suggestions.

For the moment I'm still working with a tablelayout compound control, constructed in code, which creates a nunber of "plain" android widgets. So no Android Binding there.
As soon as I move to the version which needs Android Binding, I'll let you know if I find a solution.

Cheers
Giuseppe
Reply all
Reply to author
Forward
0 new messages