Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 560
Justin Conroy Avatar asked Sep 19 '15 19:09

Justin Conroy


People also ask

How to add a custom view to default view?

Under Default View, you also have the option to create your custom view also ('New' highlighted in the screenshot). From there, you can add your custom view, add your fields, apply filter criteria and can opt it inside Default View. If my answer helped to resolve your issue, kindly verify it by clicking 'Yes'.

How do I customize views in Dynamics 365?

You can also use the customization tool in Dynamics 365 to customize views. More information: Create and edit views The following table lists the five types of views that are supported for customization. The type code of a view is stored in the SavedQuery.QueryType attribute.

How to add custom view for subgrid?

Add Custom View for subgrid 1 1.Go to the form editor where you have added the subgird. 2 2.Double Click on the subgrid (Or say Change Properties). 3 3.Go to "Data Source" section. 4 4.Change the default view as you want. More ...

Is it possible to create new columns in data view?

Otherwise, many cases it is no use creating new columns in data view and again creating the same in query view that is not as superior or user awareness as DAX 06-14-2016 10:31 AM


2 Answers

Did you try setting the LayoutParams?

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

    //Add clock view
    ClockView clockView = new ClockView(_context);
    RelativeLayout.LayoutParams layout_params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    clockView.setLayoutParams(layout_params);
    _variableLayout.addView(clockView);
}
like image 185
Byte Welder Avatar answered Oct 26 '22 10:10

Byte Welder


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.

like image 43
Justin Conroy Avatar answered Oct 26 '22 10:10

Justin Conroy