Search code examples
androiddynamicbuttondirection

Dynamic Buttons directions in Android


I made my UI with .xml, But I want to give the direction of the Button by dynamically. For Example;

 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button 
         android:id="@+id/backbutton"
         android:text="TEST123"
         android:layout_x="120px"
         android:layout_y="120px"
         android:layout_width="100px"
         android:layout_height="100px" />
    </AbsoluteLayout>

Then is my activity class I changed something by dynamically like that.

    AbsoluteLayout al = new AbsoluteLayout(this);
    Button t = new Button(this);
    t.setHeight(300);
    t.setWidth(300);
    t.setText("TEST123");
    // x y ???
    setContentView(al);

I've changed the height and width..but I couldn't find the way to changed x and y directions.


Solution

  • try setPadding(int top,int left,int right,int bottom) method.

    AbsoluteLayout al = new AbsoluteLayout(this);
        Button t = new Button(this);
        t.setHeight(300);
        t.setWidth(300);
        t.setText("TEST123");
        t.setPadding(120,120,0,0);
        setContentView(al);
    

    I am not sure how your other elements of the screen are fitting, but I just assumed that button is the only element you have in your screen.