1

Is there any way to adjust height and width of a custom component using XML attributes?

For example, I created a component and its layout.

Here is XML layout of the component:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="TextView" />

</LinearLayout>

And here is a simple class that inflates this layout:

public class CustomComponent extends LinearLayout
{
    public CustomComponent(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater)context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewGroup view = (ViewGroup)inflater.inflate(
            R.layout.custom_component, null);
        addView(view);
    }
}

The custom component is used in layout of some activity that looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <view
        class = "com.tests.CustomComponent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

The component is meant to be stretched to full size of a screen (that's why its layout parameters set to "fill_parent") but that is never happen.

Can anyone help me with this?

1 Answer 1

1

Seeing as your CustomComponent extends LinearLayout, you just need to inflate the layout directly into it (parsing this as the ViewGroup, as your CustomComponent will be the holding view):

public class CustomComponent extends LinearLayout
{
    public CustomComponent(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        inflate( context, R.layout.custom_component, this );
    }
}

If that doens't work, try supplying only the constructor that takes a context:

public class CustomComponent extends LinearLayout
{
    public CustomComponent(Context context)
    {
        super(context);
        inflate( context, R.layout.custom_component, this );
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

first solution is good , but second is not possible because when you will add view in XML layout it will alwats search for constructor (context, AttributeSet)
Thank you for a quick answer you've made my day. The first solution works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.