0

I add a view to the left of an existing view which is centred in parent. I would like the layout to update and centre both the view I added dynamically to the left of the existing view and the existing view.

I have previously looked at this question and tried to get the view to wrap_content after I have added the view which did not work using:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF, timePicker.getId());
params.addRule(RelativeLayout.CENTER_VERTICAL);
picker.setLayoutParams(params);
lytBottom.addView(picker);

RelativeLayout.LayoutParams existingParams = (RelativeLayout.LayoutParams) lytBottom.getLayoutParams();
existingParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;

and tried changing the visibility as per the top suggested comment. All the above did was wrap_content around the view originally there and completely ignores the dynamically added view.

Is there another way I could be approaching this to achieve the centering of both the new view and the old one while keeping the new view to the left of the old view?

The xml:

<android.support.percent.PercentRelativeLayout
        android:id="@+id/lytBottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/lytTop"
        android:layout_centerHorizontal="true"
        app:layout_heightPercent="30%">

        <TimePicker
            android:id="@+id/timePicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:timePickerMode="spinner"/>

    </android.support.percent.PercentRelativeLayout>
Community
  • 1
  • 1
Josh Laird
  • 6,974
  • 7
  • 38
  • 69
  • As the data I need to add is dynamic I have just aligned a view to the left in the XML and add the data in dynamically. Would still be curious to know if it is possible to do the above. – Josh Laird Aug 07 '16 at 21:21
  • Wrap both views in a new layout, center the new layout. Or add a 0x0dp "anchor" view, center this one, add both views left and right of it. – ElDuderino Aug 07 '16 at 23:46

0 Answers0