Hi,

I wanted to create my own custom layout.
While testing it, i found out that the custom attributes that i implemented 
in my layout don't get passed to the childs of my layout correctly and 
always are equal to my specified default values.

I couldn't find any suitable things on the internet about this and I'm 
really new to android development, so forgive me if it's some really dumb 
thing I did wrong.

This is the xml code of my main activity:

<?xml version="1.0" encoding="utf-8"?>
<com.myCompany.myProjectName.TestLayout
    xmlns:android="http://schemas.android.com/apk/res/android";
    
xmlns:custom="http://schemas.adroid.com/apk/res/com.myCompany.myProjectName";
    xmlns:tools="http://schemas.android.com/tools";
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Main" >
    <TextView 
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        custom:test="1"
        android:text=""
        android:background="#FFF" />
</com.myCompany.myProjectName.TestLayout>

I defined the layout attributes in res/values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TestLayout" />
    <declare-styleable name="TestLayout_LayoutParams">
        <attr name="test" format="integer" />
    </declare-styleable>
</resources>

and the code for the layout itself:

public class TestLayout extends ViewGroup
{
    public TestLayout(Context context)
    {
        super(context);
    }
    
    public TestLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        for (int i = 0; i < getChildCount(); i++)
        {
            getChildAt(i).layout(0, 0, 400, 200);
        }
    }
    
    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p)
    {
        return p instanceof LayoutParams;
    }
    
    @Override
    protected LayoutParams generateDefaultLayoutParams()
    {
        return new LayoutParams(0, 0);
    }
    
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs)
    {
        return new LayoutParams(getContext(), attrs);
    }
    
    @Override
    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
    {
        return new LayoutParams(p.width, p.height);
    }

    public static class LayoutParams extends ViewGroup.LayoutParams
    {
        public int test;
        
        public LayoutParams(int width, int height)
        {
            super(width, height);
        }
        
        public LayoutParams(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            
            TypedArray a = context.obtainStyledAttributes(attrs, 
R.styleable.RelativeLayoutEx_LayoutParams);
            
            try
            {
                this.test = 
a.getInt(R.styleable.TestLayout_LayoutParams_test, 0);
            }
            finally
            {
                a.recycle();
            }
        }
    }
}

In fact, I'm not doing much in these lines and since it's only for testing, 
the onLayout function gives every child the same values for position and 
size.
The only thing my app does in the moment is printing the "test" attribute 
of the TextView if i click on it:

public class Main extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView tv = (TextView)findViewById(R.id.textView);
        tv.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                TextView tv = (TextView)v;
                
                TestLayout.LayoutParams lp = 
(TestLayout.LayoutParams)v.getLayoutParams();
                
                tv.setText("test: " + lp.test);
            }
        });
    }
}

Well, and it always prints "test: 0", no matter what value i give it in the 
main.xml.
..has anyone around here a clue what this could be all about?


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to