Search code examples
androidandroid-custom-view

Custom View not visible


I am trying to add the imageViews to a Custom RelativeLayout programmtically. The problem is for some reason the childViews of the custom RelativeLayout aren't visible. What am I doing wrong?

public class MyViewGroup extends RelativeLayout{

    Context mContext;

    public MyViewGroup(Context context) {
        super(context);

        mContext = context;

        init();
    }

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);

        mContext = context;
        init();
    }

    public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;
        init();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }

    void init()
    {
        for(int i=0; i<2; i++)
        {
            ImageView myImage = new ImageView(mContext);

            if(i==0)
                myImage.setBackgroundColor(Color.CYAN);
            else
                myImage.setBackgroundColor(Color.MAGENTA);


            myImage.setId(i);

            RelativeLayout.LayoutParams params= new LayoutParams(50+i*70, 50+i*70);
            myImage.setLayoutParams(params);


            ImageView sImage = new ImageView(mContext);

            if(i==0)
            sImage.setBackgroundColor(Color.RED);
            else
                sImage.setBackgroundColor(Color.GREEN);


            RelativeLayout.LayoutParams p= new LayoutParams(200+i*70, 200+i*70);
            p.addRule(RelativeLayout.ALIGN_TOP | RelativeLayout.ALIGN_LEFT, myImage.getId());
            sImage.setLayoutParams(p);

            addView(myImage);
            addView(sImage);

        }

    }
}

inside layout file.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <user.com.MyViewGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

What am I missing?

Best Regards

EDIT:

        RelativeLayout.LayoutParams params= new LayoutParams(200+i*70, 200+i*70);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        myImage.setLayoutParams(params);


        ImageView sImage = new ImageView(mContext);

        sImage.setBackgroundColor(Color.RED);


        RelativeLayout.LayoutParams p= new LayoutParams(20+i*70, 20+i*70);
        p.addRule(RelativeLayout.CENTER_IN_PARENT);
        p.addRule(RelativeLayout.ALIGN_BOTTOM, myImage.getId());
        sImage.setLayoutParams(p);

        addView(myImage);
        addView(sImage);

Solution

  • you missed to call the super.onLayout(changed, l, t, r, b);, which is responsible to assign a position to its children. In a concrete implementation of ViewGroup, if the default implementation is enough for you, you can avoid to override this method. If you decide to extend ViewGroup instead, you have to provide an implementation, since it is declared as abstract. In your case you can get rid of

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    
    }