Android: Set margins in a FrameLayout programmatically- not working
Asked Answered
C

6

20

here is the code-

FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) searchPin.getLayoutParams();
params.setMargins(135, 176, 0, 0);
//params.leftMargin = 135; // also not worked 
//params.topMargin = 376;
searchPin.setLayoutParams(params);

Where ever, from xml its working-

android:layout_marginLeft="135dp"

what can be the reason? am i missing something!

-thnx

Consolatory answered 12/11, 2011 at 0:32 Comment(1)
#5402452Martyrdom
C
17

Try this:

params.gravity = Gravity.TOP;
Corody answered 5/7, 2012 at 15:59 Comment(1)
or even: params.gravity = Gravity.NO_GRAVITY;Couvade
B
12

I had a FrameLayout child of a RelativeLayout so the framework was trying to cast FrameLayout params to RelativeLayout, I solved this way

FrameLayout mylayout = (FrameLayout) view.findViewById(R.id.mylayout);
ViewGroup.MarginLayoutParams params = (MarginLayoutParams) mylayout.getLayoutParams();
params.setMargins(1, 2, 3, 4);
mylayout.setLayoutParams(params);
Blade answered 5/8, 2012 at 23:28 Comment(3)
if your FrameLayout is a child of RelativeLayout. the you've got set RelativeLayout.LayoutParams to FrameLayoutIncapacitate
agree w/Roman Black: This is the solution that worked for me. You need to set the params with the type of the parent.Expressly
Maybe @Blade didn't express himself clearly but he certainly made a point. FrameLayout.LayoutParams is not the same as ViewGroup.LayoutParamsMccammon
T
5

I think what you're missing is that, when you set programmatically the parameters for an element, those parameters are provided actually to the parent View, so that it knows how to position the element. The parameters are not set back to the element itself. Consider the following code example. Also note, that the layout parameters are of the type of the parent.

LinearLayout linearLayout = new LinearLayout(this);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.FILL_PARENT, 
     LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParams.setMargins(6,6,6,6);

Button someButton=new Button(this);
someButton.setText("some text");

linearLayout.addView(someButton, layoutParams);
Toplevel answered 12/11, 2011 at 0:41 Comment(3)
then what should I do for child element? Instead of that, do i need to add padding for parent - to properly place the child!Consolatory
no, you don't have to set padding for child element, as padding to parent. The idea is more like, you supply both (child element + child element layout params) to the parent- as in the addView line.Toplevel
Not working either, using addView with two parameters, or setting the layout parameters to the view directly results in the sameConcentric
I
3

You can try this

FrameLayout.LayoutParams srcLp = (FrameLayout.LayoutParams)searchPin.getLayoutParams();
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(srcLp.width, srcLp.height, srcLp.gravity);

lp.setMargins(srcLp.leftMargin, srcLp.topMargin, srcLp.rightMargin, srcLp.bottomMargin);

searchPin.setLayoutParams(lp);
Incapacitate answered 21/1, 2014 at 13:42 Comment(0)
C
2

simple solution-

searchPin = (ImageView) findViewById(R.id.waypts_search_pin);
searchPin.setPadding(135, 176, 0, 0);
Consolatory answered 12/11, 2011 at 10:6 Comment(1)
Well this doesn't really help other people who are searching for an answer to your question, Padding doesn't move the background on the frame, just the contents.Concentric
B
-1

Just use LayoutParams instead of FrameLayout.LayoutParams

LayoutParams params = (LayoutParams) searchPin.getLayoutParams();
params.setMargins(135, 176, 0, 0);
searchPin.setLayoutParams(params);

this worked for me

Biauriculate answered 3/7, 2012 at 15:32 Comment(1)
LayoutParams doesn't have a setMargins method. You've just imported android.widget.FrameLayout.LayoutParamsOutrigger

© 2022 - 2024 — McMap. All rights reserved.