While working to create a custom ViewGroup I ran into a problem. I have created a ViewGroup with the following code:
import android.content.Context;
import android.graphics.RectF;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
public class noteLayout extends ViewGroup {
float leftOrientationSize = 0;
float rightOrientationSize=0;
public noteLayout(Context activityContext)
{
super(activityContext);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
RelativeLayout mainParent = (RelativeLayout) getParent();
final RelativeLayout child = (RelativeLayout) getChildAt(0);
for (int i = 0; i < child.getChildCount(); i++) {
final View childOfChild = child.getChildAt(i);
if (childOfChild != null) {
childOfChild.measure(MeasureSpec.makeMeasureSpec((mainParent.getWidth() / 2) - 30, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(mainParent.getHeight(), MeasureSpec.AT_MOST));
}
}
child.measure(MeasureSpec.makeMeasureSpec((mainParent.getWidth() / 2) - 30, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec((mainParent.getHeight() / 2) - 300, MeasureSpec.AT_MOST));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int numberOfChild = getChildCount();
for (int i = 0;i<numberOfChild;i++){
View childView = getChildAt(i);
float childHeight = (float) childView.getMeasuredHeight();
float childWidth = (float) childView.getMeasuredWidth();
RectF rect = new RectF();
rect.bottom = childHeight+20;
rect.top = 20;
rect.left = 20;
rect.right = childWidth+20;
childView.layout((int) rect.left, (int) rect.top, (int) rect.right, (int) rect.bottom);
}
}
}
So I basically instantiate a new instance of my ViewGroup by doing noteLayout customViewgroup = new noteLayout();
Afterward, do customViewgroup.addView(getLayoutInflater().inflate(R.Layout.test3,null))
; to add the test3 xml layout into my CustomViewGroup. Then I get a reference to my main activity relative layout and used the addView(customViewGroup)
method of my main activity Relative Layout to add in my CustomViewGroup which contains the test3 layout.
My test3 Layout is as shown:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@android:color/holo_blue_bright"
android:id="@+id/ma"
android:elevation="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Note"
android:id="@+id/displayNote"
android:paddingBottom="10dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:ellipsize="end"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title:"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"/>
</RelativeLayout>
My problem is I don't understand how my ViewGroup is measuring the TextView within Relative Layout which it contains so here are some images that explain my concern.
So The above image are in the order as followed. The image with full on cyan background is the image of test3 in my xml layout and the second is how it actually renders. My concern is that I don't know what the heck is going on because the way the image rendered doesn't match what I have in the xml Layout. Sometimes the Note text would appear in the middle, sometimes it would disappear depending on where it is positioned on the relativelayout. However, my concern is that why does the note view get rendered so differently compared to how it is in the xml design tab? Perhaps my ViewGroup may be coded wrong?