X.X_Mass_Developer
X.X_Mass_Developer

Reputation: 727

custom LinearLayout with custom View elements shows nothing

I have a class CustomView which extends LinearLayout. I have another class CustomElement which also extends LinearLayout. When i try to use my class in XML nothing shows up.

This is my class CustomView:

private static int NUMBER_OF_ELEMENTS = 4;

public CustomView(final Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

private void init(Context context) {
        // get size for each element
    int width = getWidth() / NUMBER_OF_ELEMENTS;
    int height = getHeight() / NUMBER_OF_ELEMENTS;

    for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
        CustomElement element = new CustomElement(context);
        addView(element, width, height);
    }
}

This is my class CustomElement:

public CustomElement(final Context context) {
    super(context);
    m_context = context;
    init(context);
}

private void init(Context context) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.custom_elem, this);
}

When i now try to add my CustomView in XML it doesn't show anything. Here is my XML Code:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/layout_mainleft"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <com.package.views.CustomView
      android:id="@+id/layout_elements"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true" />

 </RelativeLayout>

Am I missing something? Any help is appreciated!

Upvotes: 0

Views: 857

Answers (2)

X.X_Mass_Developer
X.X_Mass_Developer

Reputation: 727

I was able to resolve the question with the help of Luksprog!

The problem was, that I was overwriting the onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) method without calling the super method! Therefore it didn't work!

Thanks again to Luksprog at this point.

Upvotes: 1

FrancescoAzzola
FrancescoAzzola

Reputation: 2654

As far as i know to create a custom layout you should extend ViewGroup and you have to override the method protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4). Here you implement your logic and place your customView. If you just extend the LinearLayout and don't change its behaviour in some way why do u extend it?

Upvotes: 0

Related Questions