How to position a Button in a RelativeLayout through code?

12,708

Here is a dynamic equivalent of your code :

    RelativeLayout rl = new RelativeLayout(this);
    LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    rl.setLayoutParams(params);
    Button button = new Button(this);
    button.setText("Previous");
    LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    button.setLayoutParams(params1);
    rl.addView(button);
Share:
12,708
Nishant Shah
Author by

Nishant Shah

Lead Android Developer

Updated on June 04, 2022

Comments

  • Nishant Shah
    Nishant Shah almost 2 years
    < RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
            < Button android:text="Previous" 
                android:layout_height="wrap_content" 
                android:id="@+id/MeasurePrev"           
                android:layout_alignParentBottom="true"
                android:layout_width="wrap_content">
            < / Button>
    < / RelativeLayout >
    

    Can anyone tell me how can i do this using Java code or in activity class? I do not know how to set android:layout_alignParentBottom="true". I want to implement whole view via java code.