0

I have created a TextView in XML and have set some rules for it (for example layout_bellow="something"), and made it's height set to 0, so that when a button is clicked, it's height would be set to wrap_content. I wrote the code bellow for the button responsible for resizing, and below it is the XML code i wrote for the TextView. The problem is, when i click the button, the height becomes match_parent, the layout_bellow attribute gets ignored and it is drawn from the start of the parent layout, and width (that was set to match_parent) becomes wrap_content. Whats the problem? thanks.

the Button:

    btnExpand.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, height));
        }
    });

the XML:

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/firstRow"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                .
                .
                .
            </LinearLayout>

            <TextView
                android:id="@+id/theTextView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_below="@id/firstRow"
                android:text="lorem ipsom lorem ipsom">

        </RelativeLayout>

EDIT: here's an image to demonstrate the problem

MrChips
  • 11
  • 2
  • 6
  • If you want to modify the `TextView`'s `LayoutParams`, get it from the `TextView` first - e.g., `RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) textView.getLayoutParams();` - make your changes on it, then set it back on the `TextView`. You're creating all new `LayoutParams`, so any attribute you don't explicitly set gets the default value, and any layout rules you don't add just won't happen, which is why the `layout_below` no longer works. – Mike M. Jun 08 '17 at 22:15
  • @MikeM. This seems to be my problem. But could you please write how i can change one param of the lp object (for example the height)? – MrChips Jun 08 '17 at 22:26
  • The height is a public field in `LayoutParams`, so you'd just set it directly. For example, after the line I showed above - `lp.height = height; textView.setLayoutParams(lp);`. – Mike M. Jun 08 '17 at 22:58
  • This only allows me to set my height to a specific number, but not to wrap_content. Any way to do that? – MrChips Jun 09 '17 at 01:10
  • Set it to `LayoutParams.WRAP_CONTENT`. – Mike M. Jun 09 '17 at 01:11
  • @MikeM. Awesome. It worked, thanks. – MrChips Jun 09 '17 at 01:16

1 Answers1

2

Basically the problem is you are creating new LayoutParams and setting it to the view. You have get the already set(xml) LayoutParams of the view and modify whichever you want to modify and set it back to the View.

LayoutParams layoutParams = textView.getLayoutParams();
//height = LayoutParams.WRAP_CONTENT;or 100 or whatever
layoutParams.height = LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(layoutParams);
Arun Shankar
  • 2,295
  • 16
  • 20