2

Possible Duplicate:
Set margins in a LinearLayout programmatically

I have bunch of widgets in my layout.I have set layout_marginTop in layout file but I want to call these two buttons in activity and set layout_marginLeft and change the value programmatically.i tried with getLayoutParams() but can not set margin. is it possible to change margin of widgets declared in xml file programmatically(after calling layout in the activity)?

//oncreate method

setContentView(R.layout.gamelayout);

//xml layout

    <ImageView
        android:id="@+id/gameImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
     >
    </ImageView>

    <Button
        android:id="@+id/lButton"
        android:layout_width="wrap_content"
        android:layout_height="140dip"
        android:layout_marginTop="10dip"
        android:layout_gravity="center_vertical|center_horizontal" />

    <Button
        android:id="@+id/rButton"
        android:layout_width="wrap_content"
        android:layout_height="140dip"
        android:layout_marginTop="10dip" 
        android:layout_gravity="center_vertical|center_horizontal" />

    <ImageView
        android:id="@+id/levelImage"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:layout_gravity="bottom|left"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/homeButton"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:layout_gravity="bottom|right"
        android:background="@drawable/home_btn" />
</FrameLayout>

please help me

thank you

Community
  • 1
  • 1
Hassy31
  • 2,793
  • 3
  • 21
  • 37
  • try [this](http://stackoverflow.com/questions/2481455/set-margins-in-a-linearlayout-programmatically) – keyser May 17 '12 at 09:46

4 Answers4

6

Each layout in Android has it's own subclass of general ViewGroup.LayoutParams which you should use to set layout params of widgets inside it. In your case it is FrameLayout.LayoutParams.

So as others mentioned:

FrameLayout layout = (FrameLayout) findViewById(R.layout.frame_layout);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( 
            FrameLayout.LayoutParams.FILL_PARENT, 
            FrameLayout.LayoutParams.WRAP_CONTENT); 
params.setMargins(30, 10, 0, 0); 
Button btn = (Button) findViewById(R.id.rbutton);
btn.setLayoutParams(params);
btn.requestLayout();

Also refer to documentation of setMargins:

A call to requestLayout() needs to be done so that the new margins are taken into account.

Andrey Ermakov
  • 3,298
  • 1
  • 25
  • 46
  • thanks! I think we should use params.setMargins instead of layoutParams.setMargins? am I correct? – Hassy31 May 17 '12 at 10:45
  • params.setMargins doesn't work without setting gavity? I don't understand why.anyway your code gave me the hint thanks a lot! :) – Hassy31 May 17 '12 at 11:39
2

Try Something like this :

 Button button = (Button)findViewById(R.id.widget111); 
 button.setText("foobar"); 
 FrameLayout.LayoutParams blp = new FrameLayout.LayoutParams(0, 0); 
 blp.leftMargin = 20; 
 blp.rightMargin= 30;
 button.setLayoutParams(blp); 
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
2
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

params.setMargins(10, 20, 30, 40);

Button button = new Button(this);
button.setText("some text");
layout.addView(button, params);
Ashok Domadiya
  • 1,124
  • 13
  • 31
2

this code snippet can help u

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

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

layoutParams.setMargins(30, 20, 30, 0);

Button okButton=new Button(this);
okButton.setText("some text");
ll.addView(okButton, layoutParams);

u have to call setMargins() on the LinearLayout.LayoutParams object.

you simply have to adapt this to your FrameLayout

K_Anas
  • 31,226
  • 9
  • 68
  • 81
  • thank you.but it's not work for me.I want to call the button from xml file.I've edited my question pls take a look. – Hassy31 May 17 '12 at 10:03
  • I think this is not working because of your root Layout try to change FrameLayout to RelativeLayout – K_Anas May 17 '12 at 13:10