2

How can I use Java code to change the layout_weight of my View object from XML? I've included the last thing I tried below. vynosy is my attribute with value that I want to set.

View hospVyslLineAppColor = (View) view.findViewById 
                            (R.id.hospodarsky_vysledok_line_appcolor);

hospVyslLineAppcolor.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT,
            Math.abs((float)vynosy)));

My XML:

<View
     android:id="@+id/hospodarsky_vysledok_line_appcolor"
     android:layout_width="match_parent"
     android:layout_height="20dp"
     android:layout_weight="69382"
     android:background="#a8a8a8" />
Michelle
  • 2,830
  • 26
  • 33
Tomino
  • 113
  • 2
  • 14

3 Answers3

1

You can use this for linearlayout

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT;  
params.weight = 1;
Aniruddha
  • 4,477
  • 2
  • 21
  • 39
0

You need to understand how the Layout_weights work. These are effective only if you have more than one views. Your code is correct, just you need to add one more view. Both declared in a linear layout. The layout in which you need to have the weights applied.

 View hospVyslLineAppColor = (View) view.findViewById (R.id.hospodarsky_vysledok_line_appcolor);

        hospVyslLineAppcolor.setLayoutParams(new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT,
        LayoutParams.WRAP_CONTENT,
        Math.abs((float)vynosy))); // see to it the value of vynosy is less than 1

 View hospVyslLineAppColor1 = (View) view.findViewById (R.id.hospodarsky_vysledok_line_appcolor);

        hospVyslLineAppColor1.setLayoutParams(new LinearLayout.LayoutParams(
        LayoutParams.MATCH_PARENT,
        LayoutParams.WRAP_CONTENT,
        (1 - Math.abs((float)vynosy)))); // Will complement the weight

And re-wite your XML file:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

          <!-- layout_height is 0dp as height is to be set by weight factor -->
           <View
              android:id="@+id/hospodarsky_vysledok_line_appcolor"
              android:layout_width="match_parent"
              android:layout_height="0dp" 
              android:layout_weight="0.4"
              android:background="#a8a8a8" />

          <!-- layout_height is 0dp as height is to be set by weight factor -->
           <View
              android:id="@+id/hospodarsky_vysledok_line_appcolor1"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="0.6"
              android:background="#a8a8a8" />
        </LinearLayout>
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Kailas
  • 7,350
  • 3
  • 47
  • 63
0

I think that this will help you... Set Width Programatically on SO

but i think that is not possible to change this property programmatically but is only possible to set a new Layout(View in generally) within the new property.

Community
  • 1
  • 1
alex_au
  • 240
  • 1
  • 2
  • 11