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!