Justin Conroy Justin Conroy - 4 months ago 12
Android Question

Custom View Not Showing When Dynamically Added

At a loss here. Searched far and wide and can't find an answer to this question...

I have an XML layout where I have a relative layout that I am using as a place holder for different views.

<RelativeLayout
android:id="@+id/relVariableLayoutPlaceHolder"

android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"

android:background="@color/white"
>

</RelativeLayout>


In my activity, I have a button for the user to press and when they press it, I want the custom
ClockView
to be created and dynamically added. Note that the
_variableLayout
is setting perfectly in the
onCreate
method:

public void onStartTimeClick(View view)
{
//Clean up any left over views
_variableLayout.removeAllViews();

//Add clock view
ClockView clockView = new ClockView(_context);
_variableLayout.addView(clockView);
}


Problem is that when the button is pressed, nothing happens on the screen. I can, however see it when I tap a
TextView
that is also on the screen.

I uploaded some images here of the before and after clicking the
TextView
:
http://imgur.com/a/9lebv

I would imagine this has something to do with the screen not being refreshed after the clock view is drawn but I have tried every combination of
invalidate()
and
requestLayout()
I can think of. Any ideas?

Answer

For historical purposes, I felt that I should mention that after a lot of debugging I figured out that my problem was in the onMeasure and onLayout functions of the ClockView. Basically they were poorly formed and not measuing the subviews properly for the onLayout function to handle properly.

Once I got those ironed out, everything was working as expected.

Comments