10

I have a button in my application. I want to change its position programmatically. I have created a button in XML as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button android:text="@+id/Button01" 
        android:id="@+id/Button01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginLeft="50px"
        android:layout_marginTop="10px"
        >
</Button>
</LinearLayout>

Suppose I want to set the position of the button as 100px from left (as layout_marginLeft="100px"). How can I do it programmatically? Please help me to solve the problem.

Smi
  • 13,850
  • 9
  • 56
  • 64
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • Let me come to the point... Actually My Layout contains 9 buttons...so some buttons are visible and invisible based on the condition..so i want to align(move left or right) it programmatically...pls help me – Paresh Mayani Jul 13 '10 at 05:02
  • Take a look at this answer. It shows clearly how to set the margins on a button. http://stackoverflow.com/a/4594374/525541 – MindWire Jun 27 '12 at 13:03
  • Padding and margin are not the same ... this is thread is what you are looking for https://stackoverflow.com/questions/2481455/set-margins-in-a-linearlayout-programmatically – Mojo Risin Jul 12 '10 at 13:58

4 Answers4

11

Luke, use the

RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

rel_btn.leftMargin = myXPosition;
rel_btn.topMargin = myYPosition;
rel_btn.width = buttonW;
rel_btn.height = buttonH;

myButton.setLayoutParams(rel_btn);
Leo
  • 3,003
  • 5
  • 38
  • 61
6

You need to get the view and transform into a Java object, then call setPadding on it.

some thing like this would work out

Button myBtn;
myBtn = (Button) findViewById(R.id.Button01);
myBtn.setPadding(0,100,0,0);

Read more here: https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • its not working ..showing red line (error) under setMargins function. I am using Android 1.5 – Paresh Mayani Jul 12 '10 at 12:48
  • i have also tried btnTest.setPadding(100,0,0,0); but it makes button of 100 width and text will be displyed on Right-hand side.but still not working – Paresh Mayani Jul 12 '10 at 13:01
  • yeh...this working if i set "setPadding(100,0,0,0) to the Layout as follows: LinearLayout l1 = (LinearLayout) findViewById(R.id.LinearLayout01); l1.setPadding(120, 0, 0, 0); Thanx a lot...@Pentium – Paresh Mayani Jul 12 '10 at 13:24
2

You can't use setMargin() with btn, use it with LayoutParams and then btn.setLayoutParams(params);

Dyppl
  • 12,161
  • 9
  • 47
  • 68
Arun
  • 1,407
  • 2
  • 10
  • 10
0

With Kotlin you can use the below:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btnTag = Button(this).apply {
            text = "Done"
            setOnClickListener { finish() }
        }

        // btnTag.setOnClickListener { this.finish() }

        val lp = RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT).apply {
            addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
            addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
        }

        main_layout.addView(btnTag, lp)
}

Note: The layout should be RelativeLayout

Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203