Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Button Position Programmatically

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.

like image 566
Paresh Mayani Avatar asked Jul 12 '10 11:07

Paresh Mayani


1 Answers

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);
like image 108
Leo Avatar answered Sep 23 '22 02:09

Leo