1

I have a following piece of layout in my xml file. There are other similar RelativeLayouts in my xml

    <RelativeLayout
    android:id="@+id/contentTextViewLayout"
    android:layout_weight="0.9"
    android:layout_width="fill_parent"
    android:layout_height="0dip">
    <TextView
        android:id="@+id/textViewStory1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:lineSpacingMultiplier="1.2"
        android:textColor="#000000"
        android:background="@drawable/book"/>
</RelativeLayout>

I want to change the layout_weight parameter of the RelativeLayout in my code. How can I do that?

lmcanavals
  • 2,339
  • 1
  • 24
  • 35
user1938357
  • 1,466
  • 3
  • 20
  • 33
  • duplicate: http://stackoverflow.com/questions/2305395/laying-out-views-in-relativelayout-programmatically – Jesse Jashinsky Jan 25 '13 at 21:54
  • The example you have shared has all the components being laidout via the code. In my case I have component layouts already defined in the xml. I just need to access and change one parameter(layout_weight) in my code depending on some condition. – user1938357 Jan 25 '13 at 22:04
  • 1
    `RelativeLayout` does not have an `android:layout_weight` attribute. That is for `LinearLayout`. – CommonsWare Jan 25 '13 at 22:09
  • @CommonsWare but a `RelativeLayout` inside of a `LinearLayout` can be given a `layout_wieght`, which I think is the situation here. – jcw Jan 25 '13 at 22:35
  • @jcw: Ah, OK, my apologies. – CommonsWare Jan 25 '13 at 23:04
  • Just to clarify, my RelativeLayout is inside a LinearLayout as rightly pointed by jcw. – user1938357 Jan 25 '13 at 23:07

1 Answers1

1

See the comment on the first question here:

Set the layout weight of a TextView programmatically

Substitute your relativelayout for the textview.

Edit, to spell it all out:

First inflate your outer LinearLayout:

    LayoutInflater inflater = LayoutInflater.from(context);
    LinearLayout ll = (LinearLayout)
        inflater.inflate(R.layout.some_linear_layout, null);
    // now get a reference to your relativelayout inside it
    RelativeLayout yourRL = (RelativeLayout)
        ll.findViewById(that_relative_layout);

    // parameters are width, height, weight
    yourRL.setLayoutParams(new 
        LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 1f));
Community
  • 1
  • 1
anthropomo
  • 4,100
  • 1
  • 24
  • 39
  • this helped. I am marking this as an answer however I am having issues in seeing the effect of setting the parameter. I will raise a new question for the same. – user1938357 Jan 31 '13 at 10:11