ef2011
ef2011

Reputation: 10631

Why wouldn't RelativeLayout.LayoutParams.addRule() work?

In OnCreate(), I initialize mRelLayout as follows:

mRelLayout = (RelativeLayout) findViewById(R.id.relative_layout);

Later on, I try to place a dynamically created button at the bottom of the screen:

RelativeLayout.LayoutParams layoutParams = 
  new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
mRelLayout.addView(dynamicallyCreateButton);

I am using the following layout XML file:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relative_layout"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView android:id="@+id/tv_intro1" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_alignParentTop="true"
            android:text="@string/intro1" />

            <Button android:id="@+id/btn_1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_intro1"
            android:layout_centerHorizontal="true"
            android:text="@string/button_label" />

        <TextView android:id="@+id/tv_label1"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_1"
            android:text="" />

        <TextView 
            android:id="@+id/tv_content1"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_label1"
            android:text="" />

        <TextView android:id="@+id/tv_label2"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_content1"
            android:text="" />

        <TextView android:id="@+id/tv_content2"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_label2"
            android:text="" />

    </RelativeLayout>
</ScrollView>

The problem is that the dynamicallyCreateButton always displays at the top of the screen, never at the bottom as I want it.

What am I doing wrong?

Upvotes: 2

Views: 7597

Answers (1)

bigstones
bigstones

Reputation: 15257

You're missing something:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(/*...*/);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

dynamicallyCreateButton.setLayoutParams(layoutParams); // <== missing line

mRelLayout.addView(dynamicallyCreateButton);

Upvotes: 5

Related Questions