Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting views dynamically with LayoutParams.addRule setting to the last position of the view

I'm dynamically adding views to a relative layout and programatically defining them. The views can be moved around the screen so their position is changing.

When I try to set a view (button2) to sit below another view (button1), button2 gets placed in the old location of button1 (the default location of where views get added before moved). I've linked images to hopefully convey this better.

This is the Original Layout Original Layout

Layout after Button2 is re-positionedRe-positioned Layout

I have a background LinkedList keeping track of all view changes and view attributes for the layout if that makes a difference.

Here are the code functions:

How i'm re-positioning Button1:

Buttons b = (Buttons) viewIndex;
                                positioningLayout = new RelativeLayout.LayoutParams(b.getLayoutParams());
                                positioningLayout.addRule(RelativeLayout.CENTER_VERTICAL);
                                b.setLayoutParams(positioningLayout);
                                baseLayout.addView(b);

Repositioning views below another view Code fragment:

Buttons b = (Buttons) viewIndex;
                                positioningLayout = new RelativeLayout.LayoutParams(b.getLayoutParams());
                                positioningLayout.addRule(RelativeLayout.BELOW, viewIdFromList.intValue());
                                b.setLayoutParams(positioningLayout);
                                b.invalidate();

How I'm adding the views to the layout.

uiList.addView(new Buttons(this), "BUTTON");
            setDialogView(uiList.getLast());
            showDialog(SET_ID);
            reloadUI();

setDialogView is just passing the view to the Dialog SET_ID so that I can manually assign an ID to the view (for testing). reloadUI() just finds the last view added to the background LinkedList and adds it to the relativeLayout using .addView;

If you require more code please let me know. Am I missing a call to update the view layouts after making a change to the relativeLayout child views? It seems like the view is getting re-positioned visually but the actual LayoutParams are not updating so when you set Button2 to Button1 it's getting the old position.

Is there a way to force a relative layout view re-position?

like image 579
Sevros Avatar asked Jun 08 '15 20:06

Sevros


2 Answers

I think you should try simple solution - replace b.invalidate() with b.requestLayout(). For more background check this link. I didn't test it but I hope it will work. So the code should look like:

Buttons b = (Buttons) viewIndex;
positioningLayout = new RelativeLayout.LayoutParams(b.getLayoutParams());
positioningLayout.addRule(RelativeLayout.BELOW, viewIdFromList.intValue());
b.setLayoutParams(positioningLayout);
b.requestLayout();

Or maybe you can simplify this to:

Buttons b = (Buttons) viewIndex;
((RelativeLayout.LayoutParams) b.getLayoutParams()).addRule(RelativeLayout.BELOW, viewIdFromList.intValue());
b.requestLayout();
like image 121
romtsn Avatar answered Sep 24 '22 06:09

romtsn


You seem to be in the right track but is doing a small mistake. Without more code it is quite difficult to provide the actual solution. But I will try to point out the mistake.

See, to relatively position items programmatically in a RelativeLayout you must assign unique ids to each of them.

Like,

Button b = new Button(this);
b.setId(1);

Button c = new Button(this);
c.setId(2);

Each element should have unique ids.

Now if you want to place the Button b vertically in the center,

layout.addRule(RelativeLayout.CENTER_VERTICAL);
b.setLayoutParams(layout);

Now if you want to place Button c below it,

layout.addRule(RelativeLayout.BELOW, b.getId());
layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
b.setLayoutparams(layout);

This will certainly place the button c below button b. As you said you can use a any data structure of your choice to keep track of the ids of each elements.

like image 31
Aritra Roy Avatar answered Sep 23 '22 06:09

Aritra Roy