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

Avisek Chakraborty
  • 8,229
  • 10
  • 48
  • 76

6 Answers6

17

Try this:

params.gravity = Gravity.TOP;
Druid
  • 6,423
  • 4
  • 41
  • 56
jdo59
  • 194
  • 1
  • 2
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);
sherpya
  • 4,890
  • 2
  • 34
  • 50
  • 1
    if your FrameLayout is a child of RelativeLayout. the you've got set RelativeLayout.LayoutParams to FrameLayout – Roman Black Nov 21 '13 at 10:48
  • agree w/Roman Black: This is the solution that worked for me. You need to set the params with the type of the parent. – mtbomb Nov 20 '14 at 07:44
  • Maybe @sherpya didn't express himself clearly but he certainly made a point. `FrameLayout.LayoutParams` is not the same as `ViewGroup.LayoutParams` – Aleks Nine Oct 30 '15 at 12:41
4

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);
hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • then what should I do for child element? Instead of that, do i need to add padding for parent - to properly place the child! – Avisek Chakraborty Nov 12 '11 at 08:25
  • 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. – hovanessyan Nov 12 '11 at 11:01
  • Not working either, using addView with two parameters, or setting the layout parameters to the view directly results in the same – htafoya Jun 19 '12 at 16:04
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);
Roman Black
  • 3,501
  • 1
  • 22
  • 31
2

simple solution-

searchPin = (ImageView) findViewById(R.id.waypts_search_pin);
searchPin.setPadding(135, 176, 0, 0);
Avisek Chakraborty
  • 8,229
  • 10
  • 48
  • 76
  • 10
    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. – htafoya Jun 19 '12 at 16:05
-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

stef
  • 525
  • 6
  • 14
  • 1
    LayoutParams doesn't have a setMargins method. You've just imported android.widget.FrameLayout.LayoutParams – roarster Jan 05 '14 at 18:05